Ios Restkit本地数据映射操作不支持';似乎无法保存到磁盘

Ios Restkit本地数据映射操作不支持';似乎无法保存到磁盘,ios,core-data,restkit,Ios,Core Data,Restkit,我目前正在使用RestKit版本0.26.0 我正在执行一个本地映射操作(它获取一个local.json文件,并将其解析为objective c核心数据对象)。我是这样做的: /* From: http://stackoverflow.com/questions/26556883/restkit-sync-data-base-with-local-json-file */ NSString* const MIMEType = @"application/json"; NSString*

我目前正在使用RestKit版本
0.26.0

我正在执行一个本地映射操作(它获取一个local.json文件,并将其解析为objective 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;
在我将一个实例映射到应用程序生命周期中的核心数据后,此方法将可靠地返回我在核心数据中拥有的
QDAuthenticationToken
的单个实例。不幸的是,一旦我关闭应用程序并尝试上述获取,它就会返回nil

虽然肯定还有其他可能的解释,但这让我相信这些
RKMapperOperation
实例没有成功地将这些对象写入磁盘。有人能在这方面提供帮助吗

我还可以提供设置
RKObjectManager
的代码,但我在其他几个项目中使用的restkit具有完全相同的设置代码,因此我非常怀疑问题在于我使用对象管理器的设置

****更新正确答案,感谢Wain的评论****

/*
 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;
/*
发件人:
http://stackoverflow.com/questions/26556883/restkit-sync-data-base-with-local-json-file
*/
NSString*const MIMEType=@“应用程序/json”;
NSString*resourceName=[route.pathPattern stringByReplacingOccurrencesOfString:@“/“with字符串:@”--”];
NSString*data_filePath=[[NSBundle mainBundle]pathForResource:resourceName of Type:@“json”];
NSData*data=[NSData data WITH CONTENTS OFFILE:data_filePath];
n错误*错误=nil;
id parsedData=[RKMIMETypeSerialization objectFromData:data-MIMEType:MIMEType错误:&error];
if(parsedData==nil&&error){
//分析器错误。。。
}
RKManagedObjectStore*managedObjectStore=self.objectManager.managedObjectStore;
NSManagedObjectContext*context=managedObjectStore.mainQueueManagedObjectContext;
RKManagedObjectMappingOperationDataSource*mappingDataSource=[[RKManagedObjectMappingOperationDataSource alloc]
initWithManagedObjectContext:上下文
缓存:managedObjectStore.managedObjectCache];
静态NSMutableArray*映射操作;
if(mapperOperations==nil)
{
mapperOperations=[NSMutableArray];
}
RKMapperOperation*mapperOperation=[[RKMapperOperation alloc]initWithRepresentation:parsedData
mappingsDictionary:objectMappings];
[mapperOperation setMappingOperationDataSource:mappingDataSource];
@已同步(映射操作){
[mapperOperations添加对象:mapperOperation];
}
dispatch\u async(dispatch\u get\u main\u queue()^{
__块布尔运算成功=假;
__块N错误*操作错误=nil;
[上下文执行锁定和等待:^{
n错误*映射错误=nil;
BOOL isMapped=[mapperOperation execute:&mappingError];
如果((isMapped==false)||
(映射错误!=nil))
{
operationError=映射错误;
返回;
}
NSError*saveError=nil;
BOOL saveSuccess=[context saveToPersistentStore:&saveError];
if((saveSuccess==false)||
(saveError!=nil))
{
operationError=保存错误;
返回;
}
操作成功=是;
}];
if((operationSuccess==TRUE)&&
(操作错误==nil))
{
成功(mapperOperation,mapperOperation.mappingResult);
}
其他的
{
故障(映射操作、操作错误、错误);
}
@已同步(映射操作){
NSUTEGER mapperOperation_索引=[mapperOperations indexOfObject:mapperOperation];
BOOL remove_mapperooperation=(mapperooperation_索引
RKMapperOperation
对上下文或保存上下文一无所知。执行操作时,只需执行映射即可

RKManagedObjectMappingOperationDataSource
创建对象,并在请求时将其插入到上下文中。然后,在映射过程中,
RKMapperOperation
会更新这些对象。之后不会要求数据源保存—数据源或映射器操作完成后不负责保存

因此,在映射器操作完成后,您需要显式保存上下文(以及,根据您使用的上下文,任何父级)


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

感谢您的及时响应,这正是我解决此问题所需的信息。为了完整起见,我应该在问题中发布更新的代码吗?我对Stack Overflow的最佳实践不太熟悉。如果您愿意,可以这样做,没有硬性规定
/*
 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;