MagicalRecord上下文保存不持久数据

时间:2012-09-19 13:00:50

标签: ios core-data nsmanagedobjectcontext magicalrecord

我有一个应用程序,在第一次运行时,从后端服务下载一堆数据(StackMob,但我不认为服务选择是一个因素),将其缓存到plist文件,&将plist中的数据加载到Core-Data存储中。在后续运行中,它使用相同的类来获取任何新的或更新的数据。将其同步到Core-Data存储中。我正在使用MagicalRecord而不是Apple的Core-Data堆栈&导入/同步在NSOperationQueue中执行。

该队列为8个实体中的每一个运行一种方法&其中一种方法看起来像这样......

- (void)processAppData
{
    NSString *entityName = kEntityAppData;                                          // customise for entity
    NSString *smidName = kAttribute_appdata_id;                                     // customise for entity
    NSArray *syncData = [MFGlobals cachedDataForName:entityName];
    [[NSFileManager defaultManager] removeItemAtPath:[MFGlobals cachePathForDownloadNamed:entityName]
                                               error:nil];
    if (!syncData) return;

    NSLog(@"%@ items: %i", entityName, [syncData count]);
    NSNumber *lastUpdated = [MFGlobals lastUpdatedEntityNamed:entityName];
    NSLog(@"Last updated %@: %qu", entityName, [lastUpdated unsignedLongLongValue]);

    for (NSDictionary *item in syncData) {
        AppData *object;                                                            // customise for entity
        NSString *smid = [item objectForKey:smidName];
        NSNumber *lastmoddate = [item objectForKey:kAttribute_lastmoddate];
        NSLog(@"%@ item with ID: %@ lastmoddate: %qu", entityName, smid, [lastmoddate unsignedLongLongValue]);

        object = [self dataItemFromEntity:entityName withID:smid inField:smidName];

        if (!object) {
            // we have to create a new item
            object = [NSEntityDescription insertNewObjectForEntityForName:entityName
                                                   inManagedObjectContext:localContext];
            [object setAppdata_id:smid];                                            // customise for entity
        } else {
            // we have an item that _might_ need updating
            if ([object lastmoddate] >= lastmoddate)
                continue;
        }

        [object setLastmoddate:lastmoddate];
        [object setValue:[item objectForKey:kAttribute_value]];
        NSLog(@"new/updated %@ object: %@", entityName, [object description]);

        [localContext saveNestedContexts];
        NSLog(@"%@ object updated.", entityName);

        if ([lastmoddate compare:lastUpdated] == NSOrderedDescending)
            lastUpdated = lastmoddate;
    }

    [MFGlobals setLastUpdated:lastUpdated forEntityNamed:entityName];
    NSLog(@"New last updated %@: %qu", entityName, [lastUpdated unsignedLongLongValue]);
}

如果我使用[localContext save]代替[localContext saveNestedContexts],则该过程似乎正常运行。当&和/或应该如何&我可以在我的应用程序中导航,但数据不会持久存储到sqlite数据库。

从此处获取提示MagicalRecord -- saveinBackground not persisting data?我转而使用[localContext saveNestedContexts]&它现在处理&正确保留前6个实体的数据。然而,在下一个实体的第一个记录中,它正确地保存了该记录。然后我的应用程序进入锁定状态。当我再次运行应用程序时,该实体的下一个(实际上是第一个新的)记录也会发生同样的情况。当我拥有该实体中的所有数据时,下一个(和最后一个实体)的第一个(仅当前)记录相同。

我在这里描述的方法与我成功用于Apple风格的核心数据代码的方法基本相同,只是我使用了提供NSManagedObjectContext&的MagicalRecord。 MagicalRecord在上下文中保存方法。

我也尝试使用MagicalRecord saveInBackgroundWithBlock(不使用队列)尝试这样做,但遇到了对我所有实体的第一个记录问题的锁定。

有关如何使其正常工作的任何线索?

干杯& TIA,佩德罗

0 个答案:

没有答案
相关问题