核心数据更新不保存,不会出现任何错误,为什么?

时间:2021-10-28 21:02:18

I am using a very simple piece of code to update an NSManagedObject, but the save does not make it to the persistent store (SQLite). There are no error messages and the logs look ok so I am a little lost.

我使用一段非常简单的代码来更新NSManagedObject,但是保存不会使它进入持久性存储(SQLite)。没有错误消息,日志看起来不错,所以我有点迷路。

I have scaled down the code as much as possible to try and isolate the problem as shown below.

我已尽可能缩小代码以尝试隔离问题,如下所示。

The logs tell me that the orderNumber and status are set correctly and also the debug output of the arrays are all correct, but my simple update still fails without any error at all.

日志告诉我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;

1 个解决方案

#1


1  

The orderHeads are fetched as dictionaries. The NSManagedObjectContext will not be aware of any changes made to a dictionary. Only NSManagedObjects will be managed by the NSManagedObjectContext, hence the name ;-)

orderHeads被提取为字典。 NSManagedObjectContext将不会知道对字典所做的任何更改。只有NSManagedObjects将由NSManagedObjectContext管理,因此名称;-)

Try to fetch the orderHeadObject as an NSManagedObject as this will allow you to save the changes back into the store. If you need the dictionary for JSON serialisation: Just fetch it again as a dictionary after you have made the changes. That second fetch will be very fast since all values of the object are already cached, so CoreData won't have to reload from the database.

尝试将orderHeadObject作为NSManagedObject获取,因为这将允许您将更改保存回商店。如果您需要JSON序列化的字典:在进行更改后再次将其作为字典再次获取。第二次获取将非常快,因为对象的所有值都已缓存,因此CoreData不必从数据库重新加载。

#1


1  

The orderHeads are fetched as dictionaries. The NSManagedObjectContext will not be aware of any changes made to a dictionary. Only NSManagedObjects will be managed by the NSManagedObjectContext, hence the name ;-)

orderHeads被提取为字典。 NSManagedObjectContext将不会知道对字典所做的任何更改。只有NSManagedObjects将由NSManagedObjectContext管理,因此名称;-)

Try to fetch the orderHeadObject as an NSManagedObject as this will allow you to save the changes back into the store. If you need the dictionary for JSON serialisation: Just fetch it again as a dictionary after you have made the changes. That second fetch will be very fast since all values of the object are already cached, so CoreData won't have to reload from the database.

尝试将orderHeadObject作为NSManagedObject获取,因为这将允许您将更改保存回商店。如果您需要JSON序列化的字典:在进行更改后再次将其作为字典再次获取。第二次获取将非常快,因为对象的所有值都已缓存,因此CoreData不必从数据库重新加载。