Optimize NSPredicate with MagicalRecord

0

First of all, follow my model:

Classes:

@interfaceMMEAlbum:NSManagedObject...outraspropriedades@property(nonatomic,retain)NSNumber*album_upcoming;@property(nonatomic,retain)NSSet*songs;@end--------@interfaceMMESong:NSManagedObject@property(nonatomic,retain)NSNumber*song_id;@property(nonatomic,retain)NSString*song_name;@property(nonatomic,retain)MMEAlbum*album;@end

Populatingthebank:

//ParsealbumMMEAlbum*newAlbum=[MMEAlbumMR_createInContext:self.managedObjectContext];newAlbum.album_active=[albumDicactive];newAlbum.album_banner_text=[albumDicbanner_text];...demaispropriedades//ParseSongsfor(NSDictionary*songDicinallSongs){MMESong*newSong=[MMESongMR_createInContext:self.managedObjectContext];newSong.song_id=[songDicid_song];newSong.song_name=[songDicname_song];newSong.album=newAlbum;}//Savecontext[selfsaveDefaultContext];

Let'sgototheproblem,atsomepointIneedtosearchforasongbynameviasearchBarandshowtheresultinatable:

//Essemétodoéacionadotodavezqueumaletraédigitadanoteclado-(NSArray*)searchTracksWith:(NSString*)string{NSPredicate*filterTracks=[NSPredicatepredicateWithFormat:@"song_name contains[c] %@", string];
NSArray *result = [MMESong MR_findAllWithPredicate:filterTracks];

return result;
}

I have 80,000 (eighty thousand) songs registered in the bank and when I perform the search using the above predicate besides being slow it freezes my view.

I tried to use this other predicate by searching through the parent root:

NSPredicate *filterTracks = [NSPredicate predicateWithFormat:@"ANY songs.song_name contains[c] %@", string];
NSArray *result = [MMEAlbum MR_findAllWithPredicate:filterTracks];
return result;

Improved response time, but nothing to please.

Any idea how to create a NSPredicate that is efficient for this type of query?

    
asked by anonymous 14.08.2014 / 17:01

2 answers

1

Actually the problem is not NSPredicate , this type of object was not designed to optimize the performance of your queries, it is a convenience for you to write more "readable" queries.

The problem is more competition related, probably the best way for you to resolve this would be to use a NSManagedObjectContext in concurrent mode of the thread you are using (which is very likely to be the main, the interface freezes during the execution of the query).

This is a common problem and can be more complicated, especially if you need to synchronize changes between one thread and another.

Given a search, I found two references that can help you a lot:

  • A Guide to Core Data Concurrency
  • Common Background Practices
  • I hope this helps you.

        
    18.08.2014 / 19:12
    0

    iTSangar,

    Try to put this process inside a thread

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
    });
    
        
    18.08.2014 / 18:35