Domanda

I'm doing this post after long research, but I'm really struggling for find the best solution to my problem.

I'm quite new both to resKit and CoreData...anyway I'm mapping and saving in CoreData the JSON that i receive from a web service.

Here some code:



    //ResKit and Core Data initialization here
    ...

    //Initialization RKObjectManager
    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://http://www.myWebService.com"]];
    objectManager.managedObjectStore = managedObjectStore;

    [RKObjectManager setSharedManager:objectManager];

    //Categories Mapping and descriptor
    RKEntityMapping *categoriesMapping = [RKEntityMapping mappingForEntityForName:@"XNCategory" inManagedObjectStore:managedObjectStore];


    [categoriesMapping addAttributeMappingsFromDictionary:@{
        @"id": @"idCategory",
        @"desc": @"desc",
        @"idFam":@"idFam"}];

    categoriesMapping.identificationAttributes = @[ @"idCategory" ];

    RKResponseDescriptor *categoriesDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoriesMapping 
        method:RKRequestMethodGET pathPattern:@"getData.asp" keyPath:@"categories"
        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


    //Families Mapping and descriptor
    RKEntityMapping *familiesMapping = [RKEntityMapping mappingForEntityForName:@"XNFamily" inManagedObjectStore:managedObjectStore];
    [familiesMapping addAttributeMappingsFromDictionary:@{
        @"id": @"idFam",
        @"desc": @"desc"}];

    familiesMapping.identificationAttributes = @[ @"idFam" ];

    RKResponseDescriptor *familiesDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:familiesMapping 
        method:RKRequestMethodGET pathPattern:@"getData.asp" keyPath:@"families"
        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    //Relation between the 2 mapping
    NSEntityDescription *categoryEntity = [NSEntityDescription entityForName:@"XNCategory"
        inManagedObjectContext:[managedObjectStore mainQueueManagedObjectContext]];
    NSRelationshipDescription *userRelationshipFrom = [categoryEntity relationshipsByName][@"family"];
    RKConnectionDescription *connectionUserMessageFrom = [[RKConnectionDescription alloc]
        initWithRelationship:userRelationshipFrom attributes:@{ @"idFam": @"idFam" }];
    [categoriesMapping addConnection:connectionUserMessageFrom];

    //Finally add the descriptor to the object manager
    [objectManager addResponseDescriptor:categoriesDescriptor];
    [objectManager addResponseDescriptor:familiesDescriptor];


In another view controller, when I need to download all data i just do:



    [[RKObjectManager sharedManager] getObjectsAtPath:@"getData.asp" parameters:@{kAuthKeyName : kAuthKeyValue} success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        //Success block
    }failure:^(RKObjectRequestOperation *operation, NSError *error) {
        //Failure Block
    }];

This code correctly download and automatically save to the persistent store in CoreData preserving relationship between entities.

Now my question: is there any method to avoid this auto save ? Because I need to process the data retrieved from webService and save them only when I decide it.

I've found this post very useful or, at least, gave me a good input for start researching: Best practice for temporary objects in RestKit with Core Data , but it's a post of 2012 and i don't know if something changed or if now there is a better solution.

In orther words, what I need, is a method that allow the mapping with ResKit so, all the received data, will be correctly entity-mapped respecting all the NSRelationshipDescription that I've created and manually manage that save to the persistent store.

Thanks alot in advance for your patience and help.

È stato utile?

Soluzione

Generally speaking, the answer you link to is still valid. RestKit has added no special support for not saving the context after the mapping is complete (it is still done automatically). The way RestKit creates and manages Core Data has changed though. You can still create your objects outside Core Data and then map them across into managed objects when you're ready to save. Trying to tamper with the Core Data setup created by RestKit will likely cause you issues in the future (if not immediately), and maintaining 2 different persistent stores would likely be more complex than preparing your data outside Core Data and then importing it when ready to save.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top