4
{"status":"ok",
 "count":5,
 "count_total":165,
 "pages":33,
 "posts":[{ "id":2971,
            "status":"publish",
            "title":"title1",
            "content":"",
            "date":"date",
            "categories":[{"id":5,
                           "title":"category1"},
                          {"id":7,
                           "title":"category2"}],
            "thumbnail":"url",
            "custom_fields":{"wpcf-content":["content"],
                             "wpcf-audio":["url"]}
              },  
          { "id":2974,
            "status":"publish",
            "title":"title2",
            "content":"",
            "date":"date2",
            "categories":[{"id":5,
                           "title":"category1"},
                          {"id":5,
                           "title":"category3"}
                         ],
            "thumbnail":"url",
            "custom_fields":{"wpcf-content":["content"],
                             "wpcf-audio":["url"]}
              }
          ]
}

posts.h
@interface posts : NSManagedObject
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSArray* wpcf-content;
@property (nonatomic, retain) NSArray* wpcf-audio;
@property (nonatomic, retain) NSString* id;
@property (nonatomic, retain) NSArray* categories;
@end

categories.h
@interface categories : NSManagedObject
@property (nonatomic, retain) NSString* title;
@end

Model.xcdatamodeld
entities  Posts : title (string), id (integer 64),wpcf-audio (transformable),wpcf-content       (transformable)
Categories : title (string)



NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Model.sqlite"];
[managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
[managedObjectStore createManagedObjectContexts];
RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:@"Categories" inManagedObjectStore:managedObjectStore];
[categoryMapping addAttributeMappingsFromDictionary:@{ @"title":@"title"}];
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Posts" inManagedObjectStore:managedObjectStore];
NSArray *identification =[[NSArray alloc]initWithObjects:@"id", nil];
mapping.identificationAttributes= identification;
[mapping addAttributeMappingsFromArray:@[@"title",@"id",@"custom_fields.wpcf-content",@"custom_fields.wpcf-audio"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:@"posts" statusCodes:statusCodes];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"categories" toKeyPath:@"categories" withMapping:categoryMapping]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://my.url/json"]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    posts *article = [result firstObject];
    categories *catego =[article.categories objectAtIndex:0];
    NSLog(@"Mapped the article: %@", article.title);
    NSLog(@"Mapped the article: %@", catego.title);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://my.url/"]];
[manager enqueueObjectRequestOperation:operation];

我收到此错误 W restkit.core_data:RKManagedObjectMappingOperationDataSource.m:157 Performing managed object mapping with a nil managed object cache:

无法通过标识属性更新现有对象实例。可能会创建重复的对象。

4

0 回答 0