2015-12-21 43 views
1

我目前使用RestKit版本0.26.0Restkit本地数据映射操作似乎没有保存到磁盘

我正在执行本地映射操作(它需要一个本地.json文件,并将其解析为目标c核心数据对象)。我这样做,像这样:

/* 
From: 
http://stackoverflow.com/questions/26556883/restkit-sync-data-base-with-local-json-file 
*/ 
NSString* const MIMEType = @"application/json"; 

NSString* resourceName = [route.pathPattern stringByReplacingOccurrencesOfString:@"/" withString:@"--"]; 
NSString* data_filePath = [[NSBundle mainBundle] pathForResource:resourceName ofType:@"json"]; 
NSData *data = [NSData dataWithContentsOfFile:data_filePath]; 

NSError* error = nil; 

id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error]; 
if (parsedData == nil && error) { 
    // Parser error... 
} 

RKManagedObjectStore *managedObjectStore = self.objectManager.managedObjectStore; 
RKManagedObjectMappingOperationDataSource *mappingDataSource = [[RKManagedObjectMappingOperationDataSource alloc] 
                   initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext 
                   cache:managedObjectStore.managedObjectCache]; 

static NSMutableArray<RKMapperOperation*>* mapperOperations; 
if (mapperOperations == nil) 
{ 
    mapperOperations = [NSMutableArray array]; 
} 

RKMapperOperation* mapperOperation = [[RKMapperOperation alloc] initWithRepresentation:parsedData 
                    mappingsDictionary:objectMappings]; 

[mapperOperation setMappingOperationDataSource:mappingDataSource]; 

[mapperOperations addObject:mapperOperation]; 

dispatch_async(dispatch_get_main_queue(), ^{ 
    NSError *mappingError = nil; 
    BOOL isMapped = [mapperOperation execute:&mappingError]; 

    if (isMapped && !mappingError) 
    { 
     success(mapperOperation,mapperOperation.mappingResult); 
    } 
    else 
    { 
     failure(mapperOperation,mappingError,false); 
    } 

    NSUInteger mapperOperation_index = [mapperOperations indexOfObject:mapperOperation]; 
    BOOL remove_mapperOperation = (mapperOperation_index < mapperOperations.count); 
    NSCAssert(remove_mapperOperation, @"unhandled"); 
    if (remove_mapperOperation) 
    { 
     [mapperOperations removeObjectAtIndex:mapperOperation_index]; 
    } 
}); 

return mapperOperation; 

通过请求达到其成功块的时候,一切似乎是为了 - 让我的所有对象解析如何我想,通过映射的结果通过。不仅如此,我还可以使用NSFetchRequest和我的RKObjectManagermanagedObjectStore.mainQueueManagedObjectContext来获取这些对象。例如,下面的回报我的身份验证令牌:

-(QDAuthenticationToken*)fetchAuthenticationTokenFromStore 
{ 
NSManagedObjectContext* context = [QDNetworkManager sharedInstance].objectManagerMainQueueManagedObjectContext; 

__block QDAuthenticationToken* authenticationToken = nil; 

NSFetchRequest *fetchRequest = [NSFetchRequest new]; 
[fetchRequest setFetchLimit:1]; 
[fetchRequest setEntity:[QDAuthenticationToken entityInManagedObjectContext:context]]; 

[context performBlockAndWait:^{ 

    NSError* fetchError = nil; 
    NSArray* fetchedObjects = [context executeFetchRequest:fetchRequest error:&fetchError]; 

    kRUConditionalReturn(fetchError != nil, YES); 
    kRUConditionalReturn(fetchedObjects.count != 1, NO); 

    authenticationToken = kRUClassOrNil(fetchedObjects.firstObject, QDAuthenticationToken); 

}]; 

return authenticationToken; 
} 

此方法将返回可靠我QDAuthenticationToken的单个实例我在核心数据,我在一个应用程序的生命周期映射到一个核心数据之后。不幸的是,一旦我杀了应用程序,并尝试上述获取,然后返回零。

尽管肯定还有其他可能的解释,但这导致我相信这些RKMapperOperation实例没有成功将这些对象写入磁盘。任何人都可以提供任何帮助吗?

我也可以提供我的代码来设置RKObjectManager,但我在一些其他项目中使用restkit,它们的设置代码完全相同,所以我非常怀疑问题是我的对象管理器的设置。

****更新与正确答案,由于北斗星的评论****

/* 
From: 
http://stackoverflow.com/questions/26556883/restkit-sync-data-base-with-local-json-file 
*/ 
NSString* const MIMEType = @"application/json"; 

NSString* resourceName = [route.pathPattern stringByReplacingOccurrencesOfString:@"/" withString:@"--"]; 
NSString* data_filePath = [[NSBundle mainBundle] pathForResource:resourceName ofType:@"json"]; 
NSData *data = [NSData dataWithContentsOfFile:data_filePath]; 

NSError* error = nil; 

id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error]; 
if (parsedData == nil && error) { 
    // Parser error... 
} 

RKManagedObjectStore *managedObjectStore = self.objectManager.managedObjectStore; 
NSManagedObjectContext* context = managedObjectStore.mainQueueManagedObjectContext; 
RKManagedObjectMappingOperationDataSource *mappingDataSource = [[RKManagedObjectMappingOperationDataSource alloc] 
                   initWithManagedObjectContext:context 
                   cache:managedObjectStore.managedObjectCache]; 

static NSMutableArray<RKMapperOperation*>* mapperOperations; 
if (mapperOperations == nil) 
{ 
    mapperOperations = [NSMutableArray array]; 
} 

RKMapperOperation* mapperOperation = [[RKMapperOperation alloc] initWithRepresentation:parsedData 
                    mappingsDictionary:objectMappings]; 

[mapperOperation setMappingOperationDataSource:mappingDataSource]; 

@synchronized(mapperOperations) { 
    [mapperOperations addObject:mapperOperation]; 
} 

dispatch_async(dispatch_get_main_queue(), ^{ 

    __block BOOL operationSuccess = false; 
    __block NSError* operationError = nil; 
    [context performBlockAndWait:^{ 

     NSError *mappingError = nil; 
     BOOL isMapped = [mapperOperation execute:&mappingError]; 

     if ((isMapped == false) || 
      (mappingError != nil)) 
     { 
      operationError = mappingError; 
      return; 
     } 

     NSError* saveError = nil; 
     BOOL saveSuccess = [context saveToPersistentStore:&saveError]; 

     if ((saveSuccess == false) || 
      (saveError != nil)) 
     { 
      operationError = saveError; 
      return; 
     } 

     operationSuccess = YES; 

    }]; 

    if ((operationSuccess == TRUE) && 
     (operationError == nil)) 
    { 
     success(mapperOperation,mapperOperation.mappingResult); 
    } 
    else 
    { 
     failure(mapperOperation,operationError,false); 
    } 


    @synchronized(mapperOperations) { 
     NSUInteger mapperOperation_index = [mapperOperations indexOfObject:mapperOperation]; 
     BOOL remove_mapperOperation = (mapperOperation_index < mapperOperations.count); 
     NSCAssert(remove_mapperOperation, @"unhandled"); 
     if (remove_mapperOperation) 
     { 
      [mapperOperations removeObjectAtIndex:mapperOperation_index]; 
     } 
    } 
}); 

return mapperOperation; 

回答

1

RKMapperOperation不知道的情况下或保存任何东西。当你执行这个操作时是执行一个映射,那就是全部。

RKManagedObjectMappingOperationDataSource创建对象并在请求时将它们插入到上下文中。这些对象在映射期间由RKMapperOperation进行更新。之后数据源不会被要求保存 - 数据源或映射器操作完成后不保存数据源的责任。

因此,在映射器操作完成后,您需要明确地保存上下文(以及取决于您正在使用的上下文的任何父项)。

您的提取请求正常工作,因为它不需要在返回对象之前保存上下文。

+0

感谢您的及时回复,这正是我需要的信息来解决这个问题。为了完整起见,我应该在我的问题中发布更新后的代码吗?我不太熟悉Stack Overflow的最佳实践。 –

+0

你可以,如果你想,没有硬性规则 – Wain