2013-05-05 78 views
0

我有這樣一段代碼:CoreData謂詞和獲取輸入需要的方式來長

-(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]; 
    } 
} 

在類別NSObject的:

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

而且這個操作需要一定的時間(800名藝術家實體約45秒)。這個是正常的?或者,我的方法在這裏有什麼問題嗎?

這是我的數據模型:這裏 enter image description here

回答

1

一些建議。

首先。要以有效的方式導入數據,我會按照我以前的答案:What is the most efficient way to sync Core Data with JSON API

二。在後臺線程中移動代碼可能會很好,因爲這樣可以避免主線程凍結。向用戶顯示等待指示符。在這裏,您可以利用新的CD API(自iOS 5以來)或傳統方式。一些筆記在這裏:iPhone iOS how to merge Core Data NSManagedObjectContext?

關於你的問題。我認爲進口需要這麼多時間是正常的。您執行選擇特定對象的查詢。無論如何,代碼可以在第一個鏈接後修改。

希望它有幫助。

相關問題