Optimieren dieser Core Data-Anfrage

Ich habe eine Entität in Core Data namens MusicInterest. Ich muss etwa 5000 davon gleichzeitig hinzufügen, und mein aktueller Prozess besteht darin, abzufragen, ob das MusicInterest bereits vorhanden ist, oder ein neues zu erstellen.

Es scheint, dass dies 5000 Fahrten zum Laden erfordert, um zu sehen, ob jeder Titel existiert. Es gibt natürlich auch Einschubfahrten, aber die 5000 Anfragen bremsen mich aus.

Jeder FacebookFriend wird mehrere Musikinteressen haben, und ich zähle sie mit einer Reihe von Stringtiteln auf, wobei ich den folgenden Code aufrufe.

Irgendwelche Ideen, wie man das optimiert?

+ (MusicInterest*) musicInterestForFacebookFriend:(FacebookFriend*)facebookFriend WithTitle:(NSString*)musicTitle UsingManagedObjectContext:(NSManagedObjectContext*)moc
{
    // query to see if there
    NSArray *matches = [self queryForMusicTitle:musicTitle moc:moc];

    if (([matches count] >= 1)) {
        // NSLog(@"Music already in database");
        MusicInterest *existingMusic = [matches lastObject];
        [existingMusic addLikedByObject:facebookFriend];
        return [matches lastObject];
    } else {
        // create new Music Interest
        MusicInterest *newMusic = [NSEntityDescription insertNewObjectForEntityForName:@"MusicInterest" inManagedObjectContext:moc];
        newMusic.title = musicTitle;
        [newMusic addLikedByObject:facebookFriend];
        return newMusic;
    }
}

+ (NSArray *)queryForMusicTitle:(NSString *)MusicTitle moc:(NSManagedObjectContext *)moc
{
    // query to see if there
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MusicInterest"];
    request.predicate = [NSPredicate predicateWithFormat:@"title == %@", [NSString stringWithFormat:@"%@", MusicTitle]];

    NSError *error = nil;
    NSArray *matches = [moc executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"Error querying title in Music interest. Error = %@", error);
    }
    return matches;
}

AKTUALISIEREN:

Ich habe das im Core Data-Programmierhandbuch vorgeschlagene Design verwendet und meine Zeit von 12 Sekunden auf 4 Sekunden verkürzt (in anderen Bereichen sind noch einige Optimierungen erforderlich :)

Das Handbuch enthält nur die Hälfte des Beispielcodes. Ich dachte, ich würde meine vollständige Implementierung freigeben:

musicArray = [[music componentsSeparatedByString:@", "] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                if (obj1 > obj2)
                    return NSOrderedDescending;
                else if (obj1 < obj2)
                    return NSOrderedAscending;
                return NSOrderedSame;
            }];

            if (musicArray) {

                NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MusicInterest"];
                [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"title IN %@", musicArray]];
                [fetchRequest setSortDescriptors:
                 @[[[NSSortDescriptor alloc] initWithKey: @"title" ascending:YES]]];

                NSError *fetchError = nil;
                NSArray *musicInterestMatchingTitles = [backgroundContext executeFetchRequest:fetchRequest error:&fetchError];

                if ([musicArray count] > 0) {
                    // walk musicArray and musicInterestsMatchingTitles in parallel
                    for (int i = 0; i < [musicArray count]; i++) {
                        NSString *title = musicArray[i];
                        if (i < [musicInterestMatchingTitles count]) {
                            MusicInterest *comparingMusicInterest = musicInterestMatchingTitles[i];
                            // compare each title
                            if (![title isEqualToString:comparingMusicInterest.title]) {
                                // if it doesn't exist as a ManagedObject (a MusicInterest), create one
                                MusicInterest *musicInterest = [MusicInterest createNewMusicInterestUsingManagedObjectContext:backgroundContext];
                                musicInterest.title = title;
                                [musicInterest addLikedByObject:friend];
                            } else {
                                // otherwise, just establish the relationship
                                [comparingMusicInterest addLikedByObject:friend];
                            }
                        } else {
                            // if there are no existing matching managedObjects, create one
                            MusicInterest *musicInterest = [MusicInterest createNewMusicInterestUsingManagedObjectContext:backgroundContext];
                            musicInterest.title = title;
                            [musicInterest addLikedByObject:friend];
                        }
                    }
                }
            }

        }];
        [self saveBackgroundContext:backgroundContext];

Antworten auf die Frage(1)

Ihre Antwort auf die Frage