R
R
Ruslan Bergutov2015-11-25 08:03:53
Cocoa
Ruslan Bergutov, 2015-11-25 08:03:53

How to put Managed Object from block into ManagedObjectContext?

Creating a managed object from a block crashes the application. You need to parse json in mo and put it in context

NSManagedObject *tempObject;
    NSManagedObjectContext *temporaryContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    [temporaryContext setParentContext:delegate.mainManagedObjectContext];

    for (NSDictionary* objd in objects) {
        tempObject = targetParser(objd, temporaryContext);
        [objectsArray addObject:tempObject];
    }

    mutableResult[@"objects"] = [objectsArray copy];
    
    return [mutableResult copy];
    return mutableResult;

const static id (^routesDeserializer)(NSDictionary*, NSManagedObjectContext*) = ^id (NSDictionary* d, NSManagedObjectContext* context) {

    Route* r = [Route newObjectWithContext:context];
    r.identifier = d[resourceIdKey];
    r.name = d[resourceNameKey];
    r.sequenceDescription = d[routeDescriptionKey];
    r.capacityType = d[routeCapacityTypeKey];
    r.objectHash = d[resourceHashKey];
    r.city = d[resourceCityIdKey];
    r.checkpoint = d[routeCheckpointIdsKey];

    return r;
};

What is the correct (or more elegant) way to do this?

Answer the question

In order to leave comments, you need to log in

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question