在RestKit postObject方法中post对象与它返回的RKMappingResult之间的关系是什么?

时间:2014-03-12 05:54:24

标签: ios core-data restkit nsmanagedobject restkit-0.20

我一直在查看有关RestKit的文档,但我还没有能够在RKMappingResult上找到一些细节。

我有代码创建NSManagedObject,名为newUser,并插入RestKit RKManagedObjectStore的mainQueueManagedObjectContext。然后,我使用对象管理器的postObject:path:parameters:success:failure:方法将用户发布到服务器。这似乎工作正常。

我需要在post请求完成时更新newUser托管对象上的某些字段,这些字段不会从post请求的结果中映射。传递到块中的mappingResult参数似乎具有与服务器响应中设置的所有字段正确映射的托管对象。

显然,mappingResult中的对象与我发布的对象不同,因为它们不在同一个线程中。如果我在发布之前保存newUser,请求完成时它们的objectID是否相同并返回映射结果?

我想象的是newUser和映射结果中的对象都引用了存储在CoreData中的同一个对象,这是正确的吗?我问的原因是,情况似乎并非如此。如果我发出一个帖子请求并同时保存newUser对象和mappingResult中返回的对象,我最终会在Core Data中找到两个不同的对象,一个用于newUser,另一个用于newUser映射结果。我通过更改mappingResult对象和 dispatch_async(dispatch_get_main_queue(), ^{ [newUser.managedObjectContext deleteObject:newRegisteredUser]; }); 对象上的字段并看到它们都是独立更改来确认这一点。此外,下次我获取时会提取两个用户对象。我该如何防止这种情况?

如果这是正确的,如果我崩溃会发生什么,请求还没有完成?我没有在CoreData中闲置的存根对象吗?

如果请求失败,如何删除我创建的存根对象?我可以在故障块中执行以下操作吗?

newUser

newUser,是我在mainQueue上创建的托管对象。

最后,如果我通过映射结果返回后台线程中的托管对象,我无法将返回的用户设置为UI用来获取信息的用户,因为它位于主线程上,那么如何通知用户界面更改用户?我是否应该发布通知,告知UI从Core Data重新获取活动用户?

关于重复对象的更新

我做了一些进一步的测试,当帖子返回时,我肯定会得到一个重复的对象。

我首先创建一个HNGRRegisteredUser *newUser = [NSEntityDescription insertNewObjectForEntityForName:@"RegisteredUser" inManagedObjectContext:context]; newUser.firstName = @"John" newUser.lastName = @"Smith" newUser.gender = @"Male" newUser.email = @"js@email.com" newUser.hungrosityModel = //fill in all of the attributes for a hungrosityModel, except uniqueID 并在其上设置属性:

NSError *saveError;
[newRegisteredUser.managedObjectContext saveToPersistentStore:&saveError];
if (saveError) NSLog(@"Save Error: %@", saveError);
NSLog(@"newUser objectID: %@", [newRegisteredUser.objectID URIRepresentation]);

此时,newUser.uniqueID == nil。然后我保存newUser(我假设我必须这样做,RestKit可以使用永久的objectID)来创建一个存根对象。

newUser

然后发布[[RKObjectManager sharedManager] postObject:newRegisteredUser path:@"users/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSError *saveError; HNGRRegisteredUser *returnedUser = [mappingResult firstObject]; NSLog(@"mappingResult objectID: %@", [returnedUser.objectID URIRepresentation]); [returnedUser.managedObjectContext saveToPersistentStore:&saveError]; if (saveError) NSLog(@"Error saving user upon registration: %@", saveError); } failure:^(RKObjectRequestOperation *operation, NSError *error) { [newUser.managedObjectContext deleteObject:newUser]; }]; 对象。

newUser objectID: x-coredata://B74AB613-1060-4D98-ABFC-3B4D89AB12C3/RegisteredUser/p2
mappingResult objectID: x-coredata://B74AB613-1060-4D98-ABFC-3B4D89AB12C3/RegisteredUser/p3

objectID的打印输出如下。

{
    "gender":"Male",
    "lastName":"Smith",
    "firstName":"John",
    "email":"js@email.com",
    "hungrosityModel": {
        "totalNumberOfUpdates":0,
        "peakHungrosity":0.8,
        "hungrosityFractionAtLastUpdate":0,
        "rateOfHungrosification":5e-05,
        "troughHungrosity":0.1,
        "peakUpdates":0
    }
}

发送到服务器的JSON是:

{"results": [
    {"receivedFriendRequests": [],
     "firstName": "John",
     "middleName": null,
     "hungrosityModel": {"uniqueID": 13},
     "email": "js@email.com",
     "gender": "Male",
     "lastName": "Smith",
     "sentFriendRequests": [],
     "uniqueID": 14,
     "updatedAt": "2014-03-12T17:45:01.973Z",
     "friends": [],
     "profileImageUpdatedAt": null,
     "createdAt": "2014-03-12T17:45:01.809Z"}
 ]}

从服务器返回的JSON是:

[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[HNGRRegisteredUser class] pathPattern:@"users/" method:RKRequestMethodPOST]];

RKResponseDescriptor *registrationResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[HNGRRKMappingProvider registeredUserMapping] method:RKRequestMethodPOST pathPattern:@"users/" keyPath:@"results" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor: registrationResponseDescriptor];

RKRequestDescriptor *registrationRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[HNGRRKMappingProvider registeredUserMapping] inverseMapping] objectClass:[HNGRRegisteredUser class] rootKeyPath:nil method:RKRequestMethodPOST];
[[RKObjectManager sharedManager] addRequestDescriptor:registrationRequestDescriptor];

对象的响应和请求描述符是:

RKEntityMapping *_mapping = nil;
_mapping = [RKEntityMapping mappingForEntityForName:@"RegisteredUser" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[_mapping addAttributeMappingsFromArray:@[@"uniqueID",
                                           @"createdAt",
                                           @"updatedAt",
                                           @"firstName",
                                           @"middleName",
                                           @"lastName",
                                           @"email",
                                           @"gender",
                                           @"profileImageUpdatedAt"]];
[_mapping addRelationshipMappingWithSourceKeyPath:@"hungrosityModel" mapping:[HNGRRKMappingProvider basicModelMapping]];
_mapping.identificationAttributes = @[@"uniqueID"];

用户的映射是:

D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: {
        results =     (
                    {
                createdAt = "2014-03-12T18:22:35.688Z";
                dateOfBirth = "2014-03-12T18:24:02Z";
                firstName = John;
                friends =             (
                );
                gender = Male;
                hungrosityModel =             {
                    uniqueID = 14;
                };
                lastName = Smith;
                middleName = "<null>";
                profileImageUpdatedAt = "<null>";
                receivedFriendRequests =             (
                );
                sentFriendRequests =             (
                );
                uniqueID = 14;
                updatedAt = "2014-03-12T18:22:35.856Z";
            }
        );
    }
     and targetObject: <HNGRRegisteredUser: 0xaa8c180> (entity: RegisteredUser; id: 0xa894550 <x-coredata://44B8DF7A-BDC9-45F2-A137-BFAACF4AAF88/RegisteredUser/p2> ; data: {
        createdAt = nil;
        dateOfBirth = "2014-03-12 18:24:02 +0000";
        email = "js@email.com";
        firstName = John;
        friendRequests = "<relationship fault: 0xa899dd0 'friendRequests'>";
        friends = "<relationship fault: 0xa891ea0 'friends'>";
        gender = Male;
        hungrosityModel = "0xa861980 <x-coredata://44B8DF7A-BDC9-45F2-A137-BFAACF4AAF88/BasicModel/p2>";
        hungrosityUpdateEvents = "<relationship fault: 0xa829d90 'hungrosityUpdateEvents'>";
        isLocalUser = 0;
        lastName = Smith;
        middleName = nil;
        myInvitations = nil;
        profileImageData = nil;
        profileImageThumbnailData = nil;
        profileImageUpdatedAt = nil;
        receivedInvitations = nil;
        registeredUserSettings = nil;
        savedHungrosityComments = "<relationship fault: 0xa8bc450 'savedHungrosityComments'>";
        uniqueID = nil;
        updatedAt = nil;
        user = nil;
        userSettings = nil;
    })
  T restkit.object_mapping:RKMapperOperation.m:320 Examining keyPath 'results' for mappable content...
  D restkit.object_mapping:RKMapperOperation.m:297 Found mappable collection at keyPath 'results': (
            {
            createdAt = "2014-03-12T18:22:35.688Z";
            dateOfBirth = "2014-03-12T18:24:02Z";
            firstName = John;
            friends =         (
            );
            gender = Male;
            hungrosityModel =         {
                uniqueID = 14;
            };
            lastName = Smith;
            middleName = "<null>";
            profileImageUpdatedAt = "<null>";
            receivedFriendRequests =         (
            );
            sentFriendRequests =         (
            );
            uniqueID = 14;
            updatedAt = "2014-03-12T18:22:35.856Z";
        }
    )

 D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
 T restkit.object_mapping:RKMappingOperation.m:953 Performing mapping operation: <RKMappingOperation 0xa8e4440> for 'HNGRRegisteredUser' object. Mapping values from object {
    createdAt = "2014-03-12T18:22:35.688Z";
    dateOfBirth = "2014-03-12T18:24:02Z";
    firstName = John;
    friends =     (
    );
    gender = Male;
    hungrosityModel =     {
        uniqueID = 14;
    };
    lastName = Smith;
    middleName = "<null>";
    profileImageUpdatedAt = "<null>";
    receivedFriendRequests =     (
    );
    sentFriendRequests =     (
    );
    uniqueID = 14;
    updatedAt = "2014-03-12T18:22:35.856Z";
} to object <HNGRRegisteredUser: 0xa8833b0> (entity: RegisteredUser; id: 0xa8777e0 <x-coredata:///RegisteredUser/t1FD3D8DE-75AD-4194-B157-EB6931B74BDB6> ; data: {
    createdAt = nil;
    dateOfBirth = nil;
    email = nil;
    firstName = nil;
    friendRequests =     (
    );
    friends =     (
    );
    gender = nil;
    hungrosityModel = nil;
    hungrosityUpdateEvents =     (
    );
    isLocalUser = 0;
    lastName = nil;
    middleName = nil;
    myInvitations = nil;
    profileImageData = nil;
    profileImageThumbnailData = nil;
    profileImageUpdatedAt = nil;
    receivedInvitations = nil;
    registeredUserSettings = nil;
    savedHungrosityComments =     (
    );
    uniqueID = 14;
    updatedAt = nil;
    user = nil;
    userSettings = nil;
}) with object mapping (null)

以下是相关的对象映射跟踪:

{{1}}

如果有更多信息可以帮助我知道,请告诉我。

1 个答案:

答案 0 :(得分:1)

是的,对象id将始终匹配(即使托管对象实例不同,它仍然代表相同的底层实体实例)。通常,它们与您在主线程上启动时将是相同的托管对象实例,而RestKit也会在主线程上调用您。

您描述的新实例不会发生。您发布的原始对象应直接更新。在您没有发生的情况下,因为服务器正在返回一个对象数组。这导致RestKit忽略提供的目标对象并创建新对象。在这种情况下,这是一个新对象,因为该数组包含一个项目。要解决此问题,您需要修改来自服务器的JSON,因为您无法作为映射的一部分索引到数组...

要删除,您不需要切换线程(尽管您可以在将来进行校对时使用),这是正确的方法。

您可以发布通知。我通常使用一个获取的结果控制器来管理观察变化。