Restkit对象映射两个类访问不良

时间:2012-02-17 22:08:58

标签: objective-c core-data restkit

我使用RestKit映射两类对象。当我自己做其中任何一个时,它完全没问题。但是,当我将它们召集在一起时,它会在449

中的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 个答案:

答案 0 :(得分:2)

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

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