简体   繁体   中英

iOS RestKit what is the purpose of [mappingProvider setMapping:forKeyPath:] method?

I'm working through the RestKit relationship mapping example and cannot understand what is the purpose of these method calls , or if there's a typo in the calls. What do they refer to? When will the object loader encounter content at these key paths?

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

The JSON file that is being loaded as data has 3 objects: project, tasks and user. Note that tasks is plural .

There are 3 entities defined in the core data model : User, Task and Project. These start with capital letters.

Finally, the NSManagedObject classes that are derived from the data model have relationships: Task>assignedUser and User>tasks and Project being a regular NSObject

Should the @"task" be @"tasks"?

@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

Thank you for the clarification!

The RKObjectMappingProvider setMapping:forKeyPath: method instructs the mapping provider about which object mapping to use when it encounters data at a given key path. In the case of the example and its associated JSON data, there are no actual instances of task keypaths -- so in this particular example, the [objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"] statement is not required; in another case, you might very well have something like

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

in which case it would be required. The actual tasks in the JSON stream are mapped because of the [projectMapping mapRelationship:@"tasks" withMapping:taskMapping] .

You can use RKLogConfigureByName() to inspect what RestKit is doing -- very useful. RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace) in this case would give you a "blow-by-blow" of the mapping logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM