Restkit 对象映射两个类错误访问

Posted

技术标签:

【中文标题】Restkit 对象映射两个类错误访问【英文标题】:Restkit Object Mapping Two Classes Bad Access 【发布时间】:2012-02-17 22:08:58 【问题描述】:

我正在使用 RestKit 映射两类对象。当我自己做其中任何一个时,效果都很好。但是当我将它们一起调用时,它会在449 in RKObjectMappingOperation.m 中崩溃,

[destinationSet setSet:destinationObject];

出现"EXC_BAD_ACCESS" 错误。

这是我的两种映射方法:

- (RKObjectLoader *)saves

    // Create an object manager and connect core data's persistent store to it
    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Db.sqlite"];
    objectManager.objectStore = objectStore;

    // Define our author mapping for saved places
    RKManagedObjectMapping *authorMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person"];
    [authorMapping mapAttributes:@"uid", nil];

    // Define our place mapping
    RKManagedObjectMapping *placeMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Place"];
    [placeMapping mapAttributes:@"uid",@"name",@"address", nil];

    // Now, connect the two via a save
    RKManagedObjectMapping *saveMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Save"];
    [saveMapping mapAttributes:@"uid", @"timestamp", nil];

    [saveMapping mapKeyPath:@"place" toRelationship:@"place" withMapping:placeMapping];
    [saveMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping];

    // We expect to find the place entity inside of a dictionary keyed "saves"
    [objectManager.mappingProvider setMapping:saveMapping forKeyPath:@"saves"];

    // Prepare our object loader to load and map objects from remote server, and send
    RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:@"places/saves" delegate:self];
    objectLoader.method = RKRequestMethodGET;
    [objectLoader send];

    return objectLoader;


- (RKObjectLoader *)recommends

    // Create an object manager and connect core data's persistent store to it
    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Db.sqlite"];
    objectManager.objectStore = objectStore;

    // Define our author mapping for recommended places
    RKManagedObjectMapping *authorMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person"];
    [authorMapping mapAttributes:@"uid", nil];

    // Define our place mapping
    RKManagedObjectMapping *placeMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Place"];
    [placeMapping mapAttributes:@"uid",@"name",@"address", nil];

    // Now, connect the two via a recommend
    RKManagedObjectMapping *recommendMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Recommend"];
    [recommendMapping mapAttributes:@"uid", @"timestamp", nil];

    [recommendMapping mapKeyPath:@"place" toRelationship:@"place" withMapping:placeMapping];
    [recommendMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping];

    // We expect to find the place entity inside of a dictionary keyed "recommends"
    [objectManager.mappingProvider setMapping:recommendMapping forKeyPath:@"recommends"];

    // Prepare our object loader to load and map objects from remote server, and send
    RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:@"places/recommends" delegate:self];
    objectLoader.method = RKRequestMethodGET;
    [objectLoader send];

    return objectLoader;

当我调用一个或另一个时,它会起作用。当我同时打电话时,

[self saves];
[self recommends];

它崩溃了。知道为什么吗?

【问题讨论】:

【参考方案1】:

设置objectManager.objectStore 的调用导致先前设置的objectStoreobjectManager 中释放。第一次调用[self saves] 创建并设置objectStore 对象,然后第二次调用[self recommends] 重复此操作,从而删除[self saves] 中的第一个设置。在[self saves] 开始的处理过程中的某个时间,原始(已发布)objectStore 对象正在被访问,因此崩溃。

一个潜在的解决方法是将objectStore setter 重构为一个由两种方法调用的单独方法,或者将其包装在if (!objectManager.objectStore) ... 语句中。

【讨论】:

你太棒了。非常感谢你。 +50 给你!

以上是关于Restkit 对象映射两个类错误访问的主要内容,如果未能解决你的问题,请参考以下文章

RestKit:将嵌套数组映射到对象

RestKit 相同对象上的一对一映射,除了

Restkit 无法为 keyPath 添加映射

RestKit映射成功后如何访问对象

restkit,如何在没有对象映射的情况下访问对象

Restkit 对象映射在映射过程中遇到错误