Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone RestKit如何加载本地JSON文件并将其映射到核心数据实体?_Iphone_Objective C_Ios_Json_Restkit - Fatal编程技术网

iPhone RestKit如何加载本地JSON文件并将其映射到核心数据实体?

iPhone RestKit如何加载本地JSON文件并将其映射到核心数据实体?,iphone,objective-c,ios,json,restkit,Iphone,Objective C,Ios,Json,Restkit,我正在构建一个双向dropbox同步应用程序。我从核心数据中加载对象,将其转换为JSON并将其发送到dropbox。但是,当我进行同步时,我会将一组本地JSON文件与dropbox JSON文件进行比较。如果检测到冲突,则应用同步逻辑。 作为同步逻辑的结果,可以下载远程JSON文件并替换本地JSON文件 因此,我在本地文档目录中得到了一堆JSON文件 如何使用RestKit使用我定义的映射将本地JSON文件反序列化回对象?RKTwitterCoreData从基于web的JSON创建核心数据实体。

我正在构建一个双向dropbox同步应用程序。我从核心数据中加载对象,将其转换为JSON并将其发送到dropbox。但是,当我进行同步时,我会将一组本地JSON文件与dropbox JSON文件进行比较。如果检测到冲突,则应用同步逻辑。 作为同步逻辑的结果,可以下载远程JSON文件并替换本地JSON文件

因此,我在本地文档目录中得到了一堆JSON文件

如何使用RestKit使用我定义的映射将本地JSON文件反序列化回对象?RKTwitterCoreData从基于web的JSON创建核心数据实体。我正在尝试对本地JSON文件执行同样的操作

有很多loadObjects方法,但它们似乎都可以用于web调用:

- (RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath delegate:(id<RKObjectLoaderDelegate>)delegate;
-(RKObjectLoader*)loadObjectsAtResourcePath:(NSString*)resourcePath委托:(id)委托;

谢谢大家!

下面是我用于核心数据到JSON到核心数据转换的内容

  -(void)deserializeFileAtPath:(NSString*)filePath
{
    DLog(@"Deserialize file: %@",filePath);
    NSError* error = nil;
    NSString *stringJSON = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:&error];
    if(error)
    {
        NSLog(@"Error reading from file: %@", filePath);
    }

    //restore the dictionary, as it was serialized
    NSDictionary* serializationDictionary =  [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:filePath] options:NSJSONReadingMutableContainers error:&error];

    //Here you must ensure that your object mapping exists
    [CoreDataWrapper setupCoreDataObjectMapping];

//top level object within JSON it will have one entity that you really want to deserialize. Without a wrapper, the mapper would not know what the top level entity really is
    CoreDataWrapper* wrapper = [CoreDataWrapper object];

    RKObjectMapper* mapper;
    error = nil;

//this performs deserialization. if you get errors about designated initializer not being called, you have setup a wrong object mapping. You need to define RKManagedObjectMapping for your core data classes
    mapper = [RKObjectMapper mapperWithObject:serializationDictionary 
                              mappingProvider:[RKObjectManager sharedManager].mappingProvider];
    RKObjectMappingResult* result = [mapper performMapping];

    //top level object within wrapper that holds the real payload
    RealCoreDataEntity* realCoreData = [result asObject];
    realCoreData.wrapper = wrapper;

//just in case
    [[wrapper managedObjectContext]save:nil];

//prints what we got back
    DLog(@"%@", realCoreData);

//prints any nested relationships
    for(NestedRelationshipObject* relationshipEntity in realCoreData.relationship)
    {
        DLog(@"Nested entity:%@", relationshipEntity);
    }

}
下面是如何定义嵌套的RestKit对象模型。当反序列化此结构的JSON文件时,它将自动为您创建所有嵌套关系,甚至合并托管对象上下文

+(void)setupCoreDataObjectMapping
{

RKObjectManager *objectManager = [RKObjectManager sharedManager ] ;

// Setup our object mappings    
/*!
 Mapping by entity. Here we are configuring a mapping by targetting a Core Data entity with a specific
 name. This allows us to map back Twitter user objects directly onto NSManagedObject instances --
 there is no backing model class!
 */
//********************************    
RKManagedObjectMapping* nestedRelationshipMapping = [RKManagedObjectMapping mappingForEntityWithName:@"NestedRelationshipObject"];
//UUID determines which objects get updated and which ones get created during the mapping process
nestedRelationshipMapping.primaryKeyAttribute = @"uuid";
[nestedRelationshipMapping mapKeyPathsToAttributes:
 @"IKeepTheseTheSame", @"IKeepTheseTheSame",
 @"AnotherValue",@"AnotherValue",
 //keep adding your attributes
 nil];
[objectManager.mappingProvider addObjectMapping:nestedRelationshipMapping];




//********************************    

RKManagedObjectMapping* mainPayloadMapping = [RKManagedObjectMapping mappingForEntityWithName:@"RealCoreDataEntity"];
mainPayloadMapping.primaryKeyAttribute = @"uuid";
[mainPayloadMapping mapKeyPathsToAttributes:
 @"companyName",@"companyName",
 //keep adding your attributes
 nil];


//this is the main payload. I create all of it's relationships before, and then add them to the mapping.
[mainPayloadMapping mapRelationship:@"relationshipName" withMapping:nestedRelationshipMapping];
[objectManager.mappingProvider addObjectMapping:mainPayloadMapping];


[objectManager.mappingProvider setSerializationMapping:[mainPayloadMapping inverseMapping] forClass:[YourNSManagedObjectSubclass class]];


[objectManager.mappingProvider setMapping:nestedRelationshipMapping forKeyPath:@"mainPayloadToNestedDataRelationshipName"];
[objectManager.mappingProvider setMapping:mainPayloadMapping forKeyPath:@"wrapperToMainPayloadRelationshipName"];


//this is a top level JSON object. It's name will not be identified within the object, but it's relationshipName will be. The result of deserializing this object would be an object that is being wrapped.
RKManagedObjectMapping* wrapperMapping = [RKManagedObjectMapping mappingForClass:[IconFileWrapper class]];
iconWrapperMapping.primaryKeyAttribute = @"uuid";
//    keyPath and attribute names. must be even
[iconWrapperMapping mapKeyPathsToAttributes:@"uuid",@"uuid",nil];
//keep adding your attributes
[iconWrapperMapping mapRelationship:@"relationshipName" withMapping:mainPayloadMapping];

[objectManager.mappingProvider addObjectMapping:wrapperMapping];
[objectManager.mappingProvider setSerializationMapping:[wrapperMapping inverseMapping] forClass:[YourWrapperNSManagedObjectSubclass class]];


}

这是来自RESTKit文档的,还没有尝试过,但看起来像是开始,因为他们使用了JSON字符串

你可以在这里找到它:


如果希望具有与“default”Restkit函数完全相同的行为
-(void)objectLoader:(RKObjectLoader*)objectLoader didloaddobjects:(NSArray*)objects
您可以使用它来获取珍贵的
对象
NSArray:

RKObjectMappingResult* result = [mapper performMapping];
NSArray* objects = [result asCollection];

您是否尝试将本地路径传递给loadObjectsAtResourcePath方法?谢谢@shannoga,这非常有用。我想指出的是,我必须在之后保存核心数据上下文,以使对象持久化。[[RKObjectManager sharedManager].objectStore.managedObjectContext保存:&错误];
 [[RKObjectManager sharedManager].objectStore.managedObjectContext save:&error];
RKObjectMappingResult* result = [mapper performMapping];
NSArray* objects = [result asCollection];