2013-12-15 34 views
0

我一直在摔跤與關係映射與RestKit現在幾天。我可以得到一個單一的對象,但嵌套的JSON數據數組沒有得到映射,並看着跟蹤日誌,我找不到任何錯誤。儘管它傷害了,我承認它已經得到了我最好的。我儘可能多地查看了一些例子,並根據別人在他們的情況下做了什麼嘗試了不同的解決方案,但沒有任何工作。我試圖遵循隨RestKit文檔一起提供的RKGist示例,但不幸的是關係映射部分尚未編寫,只有佔位符。RestKit .22:無法成功映射關係與嵌套的JSON

這是JSON示例響應我的工作:

{ 
"stat": { 
    "code": 200 
}, 
"data": { 
    "channel": { 
     "_id": "68413dzz39f4843t8500000d", 
     "desc": "...", 
     "name": "...", 
     "owner": { 
      "_id": "584ege8239f4883f6200000b", 
      "name": "..." 
     }, 
     "count": { 
      "invited": 0, 
      "joined": 1, 
      "subscribers": 0, 
      "posts": 0, 
      "messages": 0 
     }, 
     "access": "PUB" 
    }, 
    "affiliation": "...", 
    "member_statuses": [ 
     { 
      "channel": { 
       "_id": "68413dzz39f4843t8500000d", 
       "name": "..." 
      }, 
      "owner": { 
       "_id": "584ege8239f4883f6200000b", 
       "dname": "...", 
       "name": "..." 
      }, 
      "type": "...", 
      "message": "This is the message text", 
      "_id": "52222e3039f4884tkk00000e", 
      "taken": "2013-11-11T20:23:45.938Z", 
      "access": "PUB" 
     } 
    ] 
} 
} 

我不能發表圖片呢,因爲這是第一個問題我一直沒能找到答案。但是,這兩個相關的核心數據模型和NSManagedObject子類:

THChannel.h

我已經建立了一個關係的所謂postsTHPostpost逆的目的地。我所選擇的類型作爲To Many作爲多個帖子可以屬於一個信道

@class THPost, THUser; 

@interface THChannel : NSManagedObject 

@property (nonatomic, retain) NSString * accessCode; 
@property (nonatomic, retain) NSString * channelId; 
@property (nonatomic, retain) NSNumber * countComments; 
@property (nonatomic, retain) NSNumber * countInvited; 
@property (nonatomic, retain) NSNumber * countJoined; 
@property (nonatomic, retain) NSNumber * countMessages; 
@property (nonatomic, retain) NSNumber * countPhotos; 
@property (nonatomic, retain) NSNumber * countPosts; 
@property (nonatomic, retain) NSNumber * countSubscribers; 
@property (nonatomic, retain) NSDate * createdAt; 
@property (nonatomic, retain) NSString * desc; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) THUser *owner; 
@property (nonatomic, retain) NSSet *posts; 
@end 

@interface THChannel (CoreDataGeneratedAccessors) 

- (void)addPostsObject:(THPost *)value; 
- (void)removePostsObject:(THPost *)value; 
- (void)addPosts:(NSSet *)values; 
- (void)removePosts:(NSSet *)values; 

@end 

THPost.h

我已經設置了一個關係稱爲postTHChannelposts逆的目的地。我所選擇的類型作爲To One作爲多個帖子可以屬於一個信道

@class THChannel, THUser; 

@interface THPost : NSManagedObject 

@property (nonatomic, retain) NSString * postId; 
@property (nonatomic, retain) NSString * channelId; 
@property (nonatomic, retain) NSString * ownerTypeCode; 
@property (nonatomic, retain) NSString * ownerId; 
@property (nonatomic, retain) NSString * postTypeCode; 
@property (nonatomic, retain) NSString * messageText; 
@property (nonatomic, retain) NSDate * createdAt; 
@property (nonatomic, retain) NSDate * takenAt; 
@property (nonatomic, retain) NSString * accessTypeCode; 
@property (nonatomic, retain) THChannel *post; 
@property (nonatomic, retain) THUser *owner; 

@end 

AppDelegate.m

// Log all HTTP traffic with request and response bodies 
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 

// Log debugging info about Core Data 
RKLogConfigureByName("RestKit/CoreData", RKLogLevelDebug); 
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace); 

NSError *error = nil; 

NSURL *baseURL = [NSURL URLWithString:API_BASE_URL]; 
NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx status codes 

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseURL]; 

// Initialize managed object store 
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; 

[managedObjectStore createPersistentStoreCoordinator]; 
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyApp.sqlite"]; 
NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption: @(NO), 
          NSInferMappingModelAutomaticallyOption: @(NO) }; 
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:options error:&error]; 
if (! persistentStore) { 
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error); 
} 

[managedObjectStore createManagedObjectContexts]; 

RKEntityMapping *channelEntityMapping = [RKEntityMapping mappingForEntityForName:@"THChannel" inManagedObjectStore:managedObjectStore]; 

channelEntityMapping.identificationAttributes = @[ @"channelId" ]; 
[channelEntityMapping addAttributeMappingsFromDictionary:@{ @"_id": @"channelId", 
                  @"name": @"name", 
                  @"desc": @"desc", 
                  @"access": @"accessCode", 
                  @"nmupl": @"nonMemberUploads", 
                  @"meta.created": @"createdAt", 
                  @"affiliation": @"viewerAffiliationCode", 
                  @"canPost": @"viewerCanPost", 
                  @"count.subscribers": @"countSubscribers", 
                  @"count.comments": @"countComments", 
                  @"count.posts": @"countPosts", 
                  @"count.photos": @"countPhotos", 
                  @"count.messages": @"countMessages", 
                  @"count.joined": @"countJoined", 
                  @"count.invited": @"countInvited" }]; 

RKEntityMapping *postEntityMapping = [RKEntityMapping mappingForEntityForName:@"THPost" inManagedObjectStore:managedObjectStore]; 

postEntityMapping.identificationAttributes = @[ @"postId" ]; 
[postEntityMapping addAttributeMappingsFromDictionary:@{ @"_id": @"postId", 
                 @"channel._id": @"channelId", 
                 @"type": @"postTypeCode", 
                 @"message": @"messageText", 
                 @"meta.created": @"createdAt", 
                 @"taken": @"takenAt", 
                 @"access": @"accessTypeCode" 
                 }]; 




[channelEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"member_statuses" toKeyPath:@"posts" withMapping:postEntityMapping]]; 

NSEntityDescription *postEntity = [NSEntityDescription entityForName:@"THPost" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext]; 
NSRelationshipDescription *postRelationship = [postEntity relationshipsByName][@"post"]; 
RKConnectionDescription *connection2 = [[RKConnectionDescription alloc] initWithRelationship:postRelationship attributes:@{ @"channelId": @"channelId" }]; 
[postEntityMapping addConnection:connection2]; 

RKResponseDescriptor *channelsOverviewObjectDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:channelEntityMapping 
                           method:RKRequestMethodAny 
                         pathPattern:@"/channels/:channelID" 
                          keyPath:@"data.channel" 
                         statusCodes:successStatusCodes]; 

[manager addResponseDescriptorsFromArray:@[channelsOverviewObjectDescriptor]]; 

// Set the default store shared instance 
manager.managedObjectStore = managedObjectStore; 

[RKObjectManager setSharedManager:manager]; 

回答

0

的問題是:

[channelEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"member_statuses" toKeyPath:@"posts" withMapping:postEntityMapping]]; 

爲嵌套的數據,這是不你有什麼。你有兩種不同的字典和兩種不同類型的數據。

幸運的是,member_statuses數組包含相關通道的id,因此您可以執行外鍵映射。您還已將頻道ID映射到帖子中(這是其他必需的部分)。爲此,您需要2個響應描述符,這些描述符對相同的響應進行操作。 1個頻道和一個帖子。應該建立與通道的連接的後置映射(因此從通道映射中去除屬性映射)。

外鍵映射應該設置這樣的:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"THPost" inManagedObjectContext:managedObjectContext]; 
NSRelationshipDescription *relationship = [entity relationshipsByName][@"post"]; 
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:e 

關係屬性:@ {@ 「渠道ID」:@ 「渠道ID」}];

然後將連接添加到帖子映射中。

+0

Wain,在未來的某一天,你會回顧並懷疑你是否在這一生中有所作爲。我會繼續告訴你,是的,你做到了。你救了我的理智。 你用我需要的附加響應描述符敲擊頭部。兩個實體之間的連接正在進行,但由於缺少響應描述符,帖子未被拾取和保存。在你的解釋之後,爲什麼這些數據不被視爲嵌套數據是有道理的,但很難看到所有事情都在進行。 我很感激你花時間回答。謝謝! – user1978398