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秒)。這個是正常的?或者,我的方法在這裏有什麼問題嗎?
這是我的數據模型:這裏