3

私は自分のプロジェクトの1つでRestKitフレームワークを使用しています。オブジェクトマッピングを使用してJSONオブジェクトをコアデータグラフに変換する方法の例が含まれています。逆のことが可能かどうか迷っています-RestKitを使用するCoreDataエンティティをJSON表現に戻すことはできますか?

クラスを見つけましRKObjectSerializerたが、それを機能させることができないようです-取得したシリアル化されたオブジェクトはnilです

-(void)doCoreDataToJSONConversion
{
    Article* article = [_articles objectAtIndex:0];
    RKManagedObjectMapping* articleMapping = [RKManagedObjectMapping mappingForClass:[Article class]];
    NSAssert(articleMapping!=nil,@"article mapping is nil!");
    NSLog(@"%@",[article description]);

    RKObjectSerializer* serializer =[RKObjectSerializer serializerWithObject:article mapping:articleMapping];

    NSError* error = nil;
    NSMutableDictionary* serializedObject = [serializer serializedObject:&error];

    if(error!=nil)
    {
        NSLog(@"!!!!! Error: %@",[error localizedDescription]);
    }

    //prints nil
    NSLog(@"Serialized Object: %@", [serializedObject description]);
}

ご意見ありがとうございます!

4

2 に答える 2

1

RestKitについて正確に伝えることはできませんが、Marcus Zarraによるこの絶対に素晴らしい答えを見るだけで、すべてのCore DataグラフをJSONに簡単に変換し、元に戻すことができます。

iPhoneのJSONとコアデータ

于 2012-04-23T13:36:45.680 に答える
1

最終的に、これらのメソッドを使用してコアデータオブジェクトをJSONに変換しました。これらのオブジェクトを逆シリアル化するには、別のクラスのマッピングを定義する必要があります。RKManagedObjectMappingは、通常のオブジェクトマッピングとは少し異なります。

-(void)setupObjectSerializationMapping
{
    RKObjectManager *objectManager = [RKObjectManager sharedManager ] ;
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Article class]];

    [mapping mapAttributes:@"articleID", @"title", @"body", nil];

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

    RKObjectMapping *imageMapping = [RKObjectMapping mappingForClass:[EventImage class]];

    [imageMapping mapAttributes:@"createDate", @"localFilePath", nil];
    [objectManager.mappingProvider addObjectMapping:imageMapping];
    [objectManager.mappingProvider setSerializationMapping:[imageMapping inverseMapping] forClass:[EventImage class]];
    [objectManager.mappingProvider setMapping:imageMapping forKeyPath:@"eventImages"];

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

    RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Event class]];

    [eventMapping mapAttributes:@"createDate", @"note", nil];
    [eventMapping mapRelationship:@"eventImages" withMapping:imageMapping];

    [objectManager.mappingProvider addObjectMapping:eventMapping];
    [objectManager.mappingProvider setSerializationMapping:[eventMapping inverseMapping] forClass:[Event class]];
    [objectManager.mappingProvider setMapping:eventMapping forKeyPath:@"events"];

    //******************************** 
    //setup App user mapping
    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[AppUserSubclass class]];

    [userMapping mapAttributes:@"userID",@"firstName", @"lastName",@"localDataFilepath", nil];
    [userMapping mapRelationship:@"events" withMapping:eventMapping];
    [objectManager.mappingProvider addObjectMapping:userMapping];
    [objectManager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[AppUserSubclass class]];
    [objectManager.mappingProvider setMapping:userMapping forKeyPath:@"appUser"];



}

-(NSString*)generateLocalJSONData
{

    NSString* folderPath = [self dataFolderPath];
    // remember what the file was called. This will not include the documents directory and will be the same on all machines.
    NSString* localFileName = [self fileName];

    NSString* dataFile = [self fullDocumentsFilePath];
    self.localDataFilepath = dataFile;

    NSAssert(self.localDataFilepath!=nil,@"failed to save local data path");
    //********************************


    NSError* error = nil;

    //app user subclass is the same as AppUser, but is used to differentiate between file based mapping and core data mapping
    RKObjectMapping *serMap = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[AppUserSubclass class]];
    NSDictionary *d = [[RKObjectSerializer serializerWithObject:self mapping:serMap] serializedObject:&error];


    if(error!=nil)
    {
        NSLog(@"!!!!! Error: %@",[error localizedDescription]);
    }


    NSString* dataContents = [d JSONString];
    BOOL success =  [dataContents writeToFile:dataFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if(!success)
    {
        NSLog(@"Error writing to data file!");
    }
    [[AppUser managedObjectContext] save:nil];

    return dataFile;

}
于 2012-05-03T15:39:13.873 に答える