Desmond
Desmond

Reputation: 5001

MagicalRecord MR_importFromObject not saved

i have issues importing json with MR.

i had set mappedKeyName of name as name, order as order

This is my json

[{"order":0,"name":"out and about"},
{"order":1,"name":"entertainment"},
{"order":2,"name":"get creative"},
{"order":3,"name":"chill"},
{"order":4,"name":"get active"},
{"order":5,"name":"get connected"},
{"order":6,"name":"fresh start"},
{"order":7,"name":"totally random"},
{"order":8,"name":"my favs"}]


-(void)importCategoryData
{
    NSError* err = nil;
    NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"category" ofType:@"json"];
    NSArray *preFillDataDict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                                options:kNilOptions
                                                  error:&err];
    NSLog(@"preFillDataArray testArray: %@", preFillDataDict);

    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
    {
        for (NSDictionary *catObj in preFillDataDict)
        {
            Planningcategory *planCat = [Planningcategory MR_importFromObject:catObj inContext:localContext];
            NSLog(@"asa");
        }
    }
    completion:^(BOOL success, NSError *error) {
        NSLog(@"Success %d error %@",success,error);
    }];
}

Log given:

NO CHANGES IN ** saveWithBlock:completion: ** CONTEXT - NOT SAVING
Success 0 error (null)

Any comments or point are greatly appreciated.

Cheers

Upvotes: 1

Views: 432

Answers (2)

phi
phi

Reputation: 10733

Have you tried using MR_importFromArray instead?

[MagicalRecord saveWithBlock: ^(NSManagedObjectContext *localContext) {
    [Planningcategory MR_importFromArray:preFillDataDict];
} completion: ^(BOOL success, NSError *error) {
    // Handle success/error
}];

Upvotes: 1

lassadt
lassadt

Reputation: 21

You have to save the context.

if (![localContext save:&error]) {
    // ...
}

Upvotes: 1

Related Questions