Adding unique objects to Core Data

Posted by absolut on Stack Overflow See other posts from Stack Overflow or by absolut
Published on 2009-08-05T22:34:48Z Indexed on 2010/03/25 7:53 UTC
Read the original article Hit count: 420

Filed under:
|
|

I'm working on an iPhone app that gets a number of objects from a database. I'd like to store these using Core Data, but I'm having problems with my relationships.

A Detail contains any number of POIs (points of interest). When I fetch a set of POI's from the server, they contain a detail ID. In order to associate the POI with the Detail (by ID), my process is as follows: Query the ManagedObjectContext for the detailID. If that detail exists, add the poi to it. If it doesn't, create the detail (it has other properties that will be populated lazily).

The problem with this is performance. Performing constant queries to Core Data is slow, to the point where adding a list of 150 POI's takes a minute thanks to the multiple relationships involved.

In my old model, before Core Data (various NSDictionary cache objects) this process was super fast (look up a key in a dictionary, then create it if it doesn't exist)

I have more relationships than just this one, but pretty much every one has to do this check (some are many to many, and they have a real problem).

Does anyone have any suggestions for how I can help this? I could perform fewer queries (by searching for a number of different ID's), but I'm not sure how much this will help.

Some code:

        POI *poi = [NSEntityDescription
				insertNewObjectForEntityForName:@"POI"
				inManagedObjectContext:[(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext]];

    poi.POIid = [attributeDict objectForKey:kAttributeID];
	poi.detailId = [attributeDict objectForKey:kAttributeDetailID];
	Detail *detail = [self findDetailForID:poi.POIid];
	if(detail == nil)
	{
		detail = [NSEntityDescription
					insertNewObjectForEntityForName:@"Detail"
					inManagedObjectContext:[(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext]];
		detail.title = poi.POIid;
		detail.subtitle = @"";
		detail.detailType = [attributeDict objectForKey:kAttributeType];
	}





-(Detail*)findDetailForID:(NSString*)detailID {
NSManagedObjectContext *moc = [[UIApplication sharedApplication].delegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
										  entityForName:@"Detail" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

NSPredicate *predicate = [NSPredicate predicateWithFormat:
						  @"detailid == %@", detailID];
[request setPredicate:predicate];
NSLog(@"%@", [predicate description]);

NSError *error;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil || [array count] != 1)
{
		// Deal with error...
	return nil;
}
return [array objectAtIndex:0];
}

© Stack Overflow or respective owner

Related posts about core-data

Related posts about objective-c