我使用一段非常简单的代码来更新NSManagedObject
,但是保存不会进入持久性存储(SQLite)。没有错误消息,日志看起来还不错,所以我有点迷失。
我已尽可能缩小代码以尝试隔离问题,如下所示。
日志告诉我orderNumber和status设置正确,并且数组的调试输出都是正确的,但我的简单更新仍然失败,没有任何错误。
+ (int) synchOrderWithStatusUpdate:(NSString *)orderNumber : (int)status {
if ([NWTillHelper isDebug] == 1) {
NSNumber *statusCheck = [NSNumber numberWithInt:status];
NSLog(@"WebServices:synchOrderWithStatusUpdate:ordernumber = %@, status = %d, status2 = %@", orderNumber, status, statusCheck);
}
synchOrderErrorCode = 0;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = appDelegate.persistentContainer.viewContext;
// *** The query for all tables uses orderNumber as selection so we set that up first for re use
NSPredicate *predicateOrderNumber =[NSPredicate predicateWithFormat:@"orderNumber like[cd] %@", [NWTillHelper getCurrentOrderNumber]];
NSFetchRequest *fetchRequestOh = [[NSFetchRequest alloc] initWithEntityName:@"OrderHead"];
NSFetchRequest *fetchRequestOrp = [[NSFetchRequest alloc] initWithEntityName:@"OrderRow"];
NSFetchRequest *fetchRequestTender = [[NSFetchRequest alloc] initWithEntityName:@"Tender"];
fetchRequestOh.predicate = predicateOrderNumber;
fetchRequestOrp.predicate = predicateOrderNumber;
fetchRequestTender.predicate = predicateOrderNumber;
fetchRequestOh.resultType = NSDictionaryResultType;
fetchRequestOrp.resultType = NSDictionaryResultType;
fetchRequestTender.resultType = NSDictionaryResultType;
NSError *errorOh = nil;
NSMutableArray *orderHeads = [[context executeFetchRequest:fetchRequestOh error:&errorOh] mutableCopy];
NSError *errorOrp = nil;
NSArray *orderRows = [[context executeFetchRequest:fetchRequestOrp error:&errorOrp] mutableCopy];
NSError *errorTender = nil;
NSArray *tenderRows = [[context executeFetchRequest:fetchRequestTender error:&errorTender] mutableCopy];
if ([NWTillHelper isDebug] == 1) {
NSLog(@"WebServices:synchOrderWithStatusUpdate:orderHeadsArray: %@", [orderHeads objectAtIndex:0]);
NSLog(@"WebServices:synchOrderWithStatusUpdate:orderRowsArray: %@", orderRows);
NSLog(@"WebServices:synchOrderWithStatusUpdate:tenderRowsArray: %@", tenderRows);
}
// *** Set the status before upload since this dictates what will happen in backend
// *** regardless if synch is successful or not
NSManagedObject *orderHeadObject = nil;
orderHeadObject = [orderHeads objectAtIndex:0];
[orderHeadObject setValue:[NSNumber numberWithInt:status] forKey:@"status"];
// Save the objects to persistent store
NSError *error = Nil;
[context save:&error];
NSLog(@"Jongel Error = %@", error);
if(error !=nil) {
if([NWTillHelper isDebug] == 1) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
synchOrderErrorCode = 99;
}
return 10101;
答案 0 :(得分:1)
orderHeads被提取为词典。 NSManagedObjectContext将不会知道对字典所做的任何更改。只有NSManagedObjects将由NSManagedObjectContext管理,因此名称为: - )
尝试将orderHeadObject作为NSManagedObject获取,因为这样可以将更改保存回商店。如果您需要JSON序列化的字典:在进行更改后再次将其作为字典获取。第二次获取将非常快,因为对象的所有值都已缓存,因此CoreData不必从数据库重新加载。