Hi guys:
Is this the best way I am creating new records con my tables? Sometime with more than 40 records some are not saved, if there is a better solution please let me know.
Thank you all.
ItemsForOrders *item;
QUT1 *qut1 = [QUT1 new];
for (int i = 0; i < [allTableData count]; i++) {
item = [allTableData objectAtIndex:i];
qut1.DocEntry = uuid;
qut1.Status = @"O";
qut1.ItemCode = item.code;
qut1.Dscription = item.name;
qut1.Quantity = item.quantity;
qut1.UoM = item.uom;
qut1.Price = item.price;
id<IDataStore> dataStore = [backendless.persistenceService of:[QUT1 class]];
[dataStore save:qut1 responder:nil];
}
You could use only one ‘save’ call using saver class with one-to-many relation of QUT1 objects, for example:
class ObjectSaver : NSObject {
var objectId : String?
var data : [QUT1]?
}
and save the saver object with the array of QUT1 objects with method - sync:
func saveNewObjectsSync(data: [QUT1]) {
var saver = ObjectSaver()
saver.data = data
let dataStore = backendless.data.of(QUT1.ofClass())
var error: Fault?
saver = dataStore.save(saver, fault: &error) as! ObjectSaver
if error == nil {
print("Data has been saved: \(saver.objectId)")
}
else {
print("Server reported an error: \(error)")
}
}
or async:
func saveNewObjectsAsync(data: [QUT1]) {
let saver = ObjectSaver()
saver.data = data
let dataStore = backendless.data.of(QUT1.ofClass())
dataStore.save(
saver,
response: { (result: AnyObject!) -> Void in
print("Contact has been saved: \((result as! ObjectSaver).objectId)")
},
error: { (fault: Fault!) -> Void in
print("Server reported an error: \(fault)")
})
}
Thank you so much Vyacheslav Vdovichenko,
Can you post, how to hande my QUT1 object to Data object in Objective C?
Please, here the saver class:
@interface ObjectSaver : NSObject
@property (strong, nonatomic) NSString *objectId;
@property (strong, nonatomic) NSArray<QUT1*> *data;
@end
@implementation ObjectSaver
@end
and sync method:
-(void)saveNewObjectsSync:(NSArray<QUT1*> *)data {
ObjectSaver *saver = [ObjectSaver new];
saver.data = data;
id <IDataStore> dataStore = [backendless.persistenceService of:QUT1.class];
Fault *error = nil;
saver = [dataStore save:saver fault:&error];
if (!error) {
NSLog(@"Data has been saved: %@", saver.objectId);
}
else {
NSLog(@"Server reported an error: %@", error);
}
}
Hope you implement an async method yourself.
Sure, thank you so much!!!