0

I have this piece of code:

-(void)createArtistsFromArray:(NSArray*)jsonAttrArr {

    NSError *fetchError = nil;
    if (jsonAttrArr) {

        for(NSMutableDictionary *dic in jsonAttrArr) {

            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            [fetchRequest setEntity:[NSEntityDescription entityForName:@"Artists" inManagedObjectContext:[sharedAppDelegate managedObjectContext]]];
            NSPredicate *predicateById = [NSPredicate predicateWithFormat:@"artistId == %@", [dic objectForKey:@"artistId"]];
            [fetchRequest setPredicate:predicateById];

            NSArray *categorisItems = [[sharedAppDelegate managedObjectContext] executeFetchRequest:fetchRequest error:&fetchError];

            Artists *artistObj = [NSObject returnItemUsingFetchingResults:categorisItems andEntityName:@"Artists"];
            [self setAttrForEntity:artistObj withDictionary: dic];
            [self createElementEntityFromDictionary:dic andArtist:artistObj];


        }

    }
    NSError *error;
    if (![[sharedAppDelegate managedObjectContext] save:&error])
        NSLog(@"Error saving - error:%@" , error);

}

-(IBAction)createElementEntityFromDictionary:(id)sender andArtist:(Artists*)aObj {

    NSMutableOrderedSet* setWithElements = [[NSMutableOrderedSet alloc] init];
    NSError *fetchError = nil;

        NSArray *elements = [(NSDictionary*)sender objectForKey:@"elements"];

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Element" inManagedObjectContext:[sharedAppDelegate managedObjectContext]]];

        int forCounter = 0;
        for (NSDictionary *elementInputDictionary in elements) {

            [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"elementId == %i", [[elementInputDictionary objectForKey:@"elementId"] integerValue]]];
            NSArray *items = [[sharedAppDelegate managedObjectContext] executeFetchRequest:fetchRequest error:&fetchError];

            Element* outputElement = [NSObject returnItemUsingFetchingResults:items andEntityName:@"Element"];
            [outputElement setCatId:aObj.artistId];
            [outputElement setOrderNo:forCounter];

            [self setAttrForEntity:outputElement withDictionary: elementInputDictionary];

            outputElement.images = [self setImagesToElement:outputElement fromList:[elementInputDictionary objectForKey:@"imagesList"]];
            [setWithElements addObject: outputElement];
            forCounter++;


        aObj.elements = setWithElements;
    }

    NSError *error;
    if (![[sharedAppDelegate managedObjectContext] save:&error]) {
        NSLog(@"Error saving - error:%@" , error);
    }

}

-(void)setAttrForEntity:(id)entityName withDictionary:(NSDictionary*)attrDictionary {
    NSDictionary *attributes = [[entityName entity] attributesByName];
    for (NSString *attribute in attributes) {
        id value = [attrDictionary objectForKey:attribute];
        if (!value) continue;
        [entityName setValue:value forKey:attribute];
    }
}

In Category NSObject:

+(id)returnItemUsingFetchingResults:(NSArray*)fResults andEntityName:(NSString*)name {
    if([fResults isEmpty])
        return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:[sharedAppDelegate managedObjectContext]];
    else
        return [fResults firstObject];
}

And this operation take some time (for 800 Artists entity about 45 seconds). This is normal? Or maybe there something wrong with my approach here?

This is my data model: enter image description here

Jakub
  • 13,712
  • 17
  • 82
  • 139

1 Answers1

1

Few suggestions here.

First. To import data in a efficient manner I would follow my previous answer on it: What is the most efficient way to sync Core Data with JSON API.

Second. Moving the code in a background thread could be good since in this way you avoid main thread to freeze. Display a wait indicator to the user. Here you can take advantage of new CD API (since iOS 5) or the traditional way. Some notes here: iPhone iOS how to merge Core Data NSManagedObjectContext?.

About your question. I think it's normal the import takes so much time. You perform queries selecting specific objects. Anyway the code can be modified following the first link.

Hope it helps.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190