2012-03-07 22 views
1

我正在通過RestKit關係映射示例工作,並且無法理解這些方法調用的目的是什麼,或者如果在調用中存在拼寫錯誤。他們指的是什麼?對象加載器何時會在這些關鍵路徑上遇到內容?iOS RestKit [mappingProvider setMapping:forKeyPath:]方法的目的是什麼?

[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"]; 
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"]; 
[objectManager.mappingProvider setMapping:projectMapping forKeyPath:@"project"]; 

正被加載數據的JSON文件有3個對象:項目,任務和用戶。請注意,任務是複數

在覈心數據模型中定義了3個實體:用戶,任務和項目。這些以大寫字母開頭。

最後,從數據模型得出的NSManagedObject類有關係:任務> assignedUser和用戶>任務和項目作爲一個普通的NSObject

如若@「任務」是@「任務」?

@implementation RKRelationshipMappingExample 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:gRKCatalogBaseURL]; 
     objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKRelationshipMappingExample.sqlite"]; 


     RKManagedObjectMapping* taskMapping = [RKManagedObjectMapping mappingForClass:[Task class]]; 
     [taskMapping mapKeyPath:@"id" toAttribute:@"taskID"]; 
     [taskMapping mapKeyPath:@"name" toAttribute:@"name"]; 
     [taskMapping mapKeyPath:@"assigned_user_id" toAttribute:@"assignedUserID"]; 
     taskMapping.primaryKeyAttribute = @"taskID"; //uniquely identifies the record for update purposes 

     RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class]]; 
     [userMapping mapAttributes:@"name", @"email", nil]; 
     [userMapping mapKeyPath:@"id" toAttribute:@"userID"]; 
     userMapping.primaryKeyAttribute = @"userID"; 


     [objectManager.mappingProvider setMapping:userMapping forKeyPath:@"user"]; 
     [objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"]; 

     [userMapping mapRelationship:@"tasks" withMapping:taskMapping];   
     [taskMapping mapRelationship:@"assignedUser" withMapping:userMapping]; 

     [taskMapping connectRelationship:@"assignedUser" withObjectForPrimaryKeyAttribute:@"assignedUserID"]; 


     // NOTE - Project is not backed by Core Data 
     RKObjectMapping* projectMapping = [RKObjectMapping mappingForClass:[Project class]]; 
     [projectMapping mapKeyPath:@"id" toAttribute:@"projectID"]; 
     [projectMapping mapAttributes:@"name", @"description", nil]; 
     [projectMapping mapRelationship:@"user" withMapping:userMapping]; 
     [projectMapping mapRelationship:@"tasks" withMapping:taskMapping]; 
     [objectManager.mappingProvider setMapping:projectMapping forKeyPath:@"project"]; 



    } 

    return self; 
} 

//more code 
@end 

謝謝您的澄清!

回答

1

RKObjectMappingProvider setMapping:forKeyPath:方法指示映射提供程序在給定鍵路徑上遇到數據時要使用哪個對象映射。在示例及其關聯的JSON數據的情況下,沒有task關鍵路徑的實際實例 - 因此在此特定示例中,不需要[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"]語句;在另一種情況下,你很可能會碰到這樣的

"task":{"id":10,"name":"Some task", "assigned_user_id":5} 

在這種情況下需要。由於[projectMapping mapRelationship:@"tasks" withMapping:taskMapping],JSON流中的實際任務被映射。

您可以使用RKLogConfigureByName()來檢查RestKit正在做什麼 - 非常有用。在這種情況下,RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace)會給你一個映射邏輯的「一擊一擊」。

相關問題