Question

Je suis cartographie de deux classes d'objets avec RestKit. Quand je fais l'un d'eux par eux-mêmes, cela fonctionne parfaitement bien. Mais quand je les appelle ensemble, il va se planter sur la ligne 449 dans RKObjectMappingOperation.m,

[destinationSet setSet:destinationObject];

Avec une erreur de "EXC_BAD_ACCESS".

Voici mes deux méthodes de cartographie:

- (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;
}

Quand j'appelle un ou l'autre, cela fonctionne. Quand j'appelle les deux,

[self saves];
[self recommends];

Il se plante. Toute idée pourquoi?

Était-ce utile?

La solution

L'appel à objectManager.objectStore ensemble est à l'origine du objectStore défini précédemment à être libéré de objectManager. Le premier appel, [self saves] crée et définit un objet objectStore, puis deuxième appel [self recommends] répète ce, en enlevant ainsi le premier ensemble de [self saves]. Quelque temps pendant le traitement a commencé par [self saves], l'objet objectStore d'origine (sortie) est en cours d'accès, d'où l'accident.

Un correctif potentiel serait de factoriser le poseur de objectStore dans une méthode distincte qui est appelée par les deux méthodes, ou l'envelopper dans une déclaration if (!objectManager.objectStore) { ... }.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top