2012-02-17 62 views
2

我用RestKit映射兩類對象。當我自己做他們中的任何一個時,它都能很好地工作。但是,當我把它們放在一起,它會在RKObjectMappingOperation.m崩潰在線449Restkit對象映射兩類不良訪問

[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]; 

它崩潰。任何想法爲什麼?

回答

2

致電objectManager.objectStore導致先前設置的objectStoreobjectManager中釋放。第一個電話[self saves]創建並設置一個objectStore對象,然後第二個電話[self recommends]重複此操作,從而刪除[self saves]中的第一個集合。在處理開始時的一段時間,由[self saves]開始,正在訪問原始(已發佈)的對象,因此崩潰。

可能的修復方法是將objectStore setter重構爲單獨的方法,該方法由兩種方法調用,或將其包裝在if (!objectManager.objectStore) { ... }語句中。

+0

你很棒。非常感謝你。 +50給你! – john 2012-02-22 22:51:02