Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-03-2010, 09:36 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 13
msf5042 is on a distinguished road
Default Managed object: efficiently delete duplicates?

I have an app that loops through an XML file and creates a managed object from the attributes. The app does this every time it starts. The XML file is large so efficiency is important. One of the attributes for an object is "QID". I need to figure out the best way to avoid multiple objects of the same QID. As it is now, I am looping through the XML attributes and when I get to the QID attribute I do a fetch request to see if it already exists, and if it does then I delete the object and continue. This takes forever because the fetch is happening a few hundred times. What is a good way to handle this? Any suggestions are appreciated. I am not a noob nor a jedi with Obj-C. A code snippet showing what I am doing is below.

Code:
for(NSDictionary *d in query) {
	if([(NSString *)[d valueForKey:@"nodeName"] isEqualToString:@"Q"]) {
		Question *q = (Question *)[NSEntityDescription 
			insertNewObjectForEntityForName:@"Question" 
			inManagedObjectContext:managedObjectContext];
		for(NSDictionary *d in (NSArray *)[d objectForKey:@"nodeChildArray"]) {
			[q setValue:[d valueForKey:@"nodeContent"] 
				forKey:[d valueForKey:@"nodeName"]];
		}
		NSFetchRequest *request = [[NSFetchRequest alloc] init];
		NSEntityDescription *entity = [NSEntityDescription 
			entityForName:@"Question" 
			inManagedObjectContext:managedObjectContext];
		NSPredicate *predicate = [NSPredicate 
			predicateWithFormat:@"(QID = %@)", q.QID];
		[request setEntity:entity];
		[request setPredicate:predicate];
		NSArray *array = [managedObjectContext 
			executeFetchRequest:request error:&error];
		if (array != nil && [array count] > 0)
		{
			for(Question *q2 in array) {
				if(![q2 isEqual:q]) {
					[managedObjectContext deleteObject:q2];
				}
			}
		}
}
msf5042 is offline   Reply With Quote
Old 09-04-2010, 01:16 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 16
Stubborn Dreams is on a distinguished road
Default

Quote:
The app does this every time it starts.
Bad data/incorrect data is a pain... However, processing to eliminate duplicates manually every time on app startup does not seem right to me.

Is this XML file shipped with the app or is it dynamic (downloaded from the web)?

If the XML file is dynamic (from the web), as always, I would greatly prefer processing the XML file using a server-side script to remove duplicates, as this offloads processing that would be needlessly duplicated across devices.

If the XML file is not dynamic and the data does not change, you should ship a CoreData database (ideally sqlite) with your app.

If the XML file is not dynamic and the data does change, you should ship a CoreData database with the entities that do not change separated (if possible) from the entities that do change.
__________________
My collaborations:
Stubborn Dreams is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 341
11 members and 330 guests
bignoggins, carlandrews, flamingliquid, hzwegjxg, ilmman, iram91419, linkmx, nadav@webtview.com, Objective Zero, stanny, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:45 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0