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 > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 08-19-2011, 02:22 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Menidi, Athens, Greece
Age: 25
Posts: 102
Soulstorm is on a distinguished road
Default Core Data Performance issues

I am creating an application that fetches data from a server (like an online address book) and uses it on the iPhone. Communication and fetching data work fine.

However, problems come into play when using Core Data to store the fetched info. (NOTE: I use SQLite as type of storage) Here is my set up and classes:

-- DSAccount. This is a subclass for NSObject. Holds fetched info from accounts fetched from the server (accountID, telephone, mail, etc). I use this class to display info inside a table view.
-- CachedAccount. This is a subclass of NSManagedObject, for Core Data. Holds exactly the same information as the DSAccount, only that I use that for storage.

Way of storing data:

-- Download the info from the server and make DSAccounts for displaying the information in the table view.
-- Then, for each DSAccount, I first check if an account with the same account ID already exists in the database (using predicates) and if there is not, I insert a Managed Object inside the core data database and populate its properties from the properties of the DSAccount. Then, I save.

However, I am experiencing performance issues on my device (even on my computer!) while saving. Consider that I fetching approx 9000 accounts from the online database, that I must save into my local core data database at once.

I feel that the way I have set up my data save store has problems. Can anyone maybe recommend me a better approach?
__________________
Visit me at Oramind Projects
Soulstorm is offline   Reply With Quote
Old 08-19-2011, 04:45 AM   #2 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

First of all you shouldn't have 2 objects for the same thing. Use only the NSManagedObject.

After you have refactored your code to only use one object per record you can go ahead and implement a NSFetchedResultsController for some super handy help with that UITableView of yours.

At this point you will have to stop and re-assess the perfornance issues and try to figure out where exactly they are. You will probably find that you need to download and process just chunks of the 9000 records. Maybe something like 200 at the time (your own testing will have to tell).

When this is all done in background threads the app should feel a lot faster and your tableview will be updated automatically by the fetched resultscontroller.

failing that post some code!
sneaky is offline   Reply With Quote
Old 08-19-2011, 11:09 AM   #3 (permalink)
vfr
Registered Member
 
Join Date: Jul 2011
Posts: 32
vfr is on a distinguished road
Default

Quote:
Then, for each DSAccount, I first check if an account with the same account ID already exists in the database (using predicates) and if there is not, I insert a Managed Object inside the core data database and populate its properties from the properties of the DSAccount. Then, I save.
Make sure that you have an index on the account ID property. Then use the -countForFetchRequest:error: method on the NSManagedObjectContext to see if the account ID exists.

If this still isn't fast enough, then you need to restructure the Core Data model so that account IDs are their own model object with a one-to-one relationship to the account info model object.

If that still isn't fast enough, then you need to forgo Core Data and use SQLite directly. Just make sure that you have an index on the account ID in all of these cases.
vfr is offline   Reply With Quote
Old 08-26-2011, 02:14 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default

Core data allows you to batch process, I don't know if that helps;

ie:

Code:

#pragma mark -
#pragma mark Core Data
- (NSFetchedResultsController *)fetchedResultsController 
{
    
	NSManagedObjectContext *context = self.managedObjectContext;
	
    if (fetchedResultsController != nil) 
	{
        return fetchedResultsController;
    }
    
    NSFetchRequest *fetchRequest	= [[NSFetchRequest alloc] init];
    NSEntityDescription *entity		= [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sort			= [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    
    
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];    
    [fetchRequest setFetchBatchSize:20];
    
    
    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController	= theFetchedResultsController;
    fetchedResultsController.delegate = self;
    
    [fetchRequest release];
    [theFetchedResultsController release];
	[sort release];
    
    return fetchedResultsController;    
}
zardon is offline   Reply With Quote
Old 10-20-2011, 10:17 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 113
jonusx is on a distinguished road
Default

Fetch all the IDs at once and store it in an array, then just compare against the array.

This is how I work it and how apple recommends it.
__________________
What do you have to share? Share and view pics Oneshare

Play Magic: The Gathering? Try The Sylvan Archives!

Warhammer 40k your thing? W40k Manager is your thing.

Like karaoke? Try iSing Karaoke Locator
jonusx 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: 393
15 members and 378 guests
Absentia, bignoggins, Diligent, dre, epaga, hussain1982, LunarMoon, momolgtm, Newbie123, omagod, Paul10, pinacate, revg, skog, taylor202
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,643
Threads: 94,110
Posts: 402,860
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Diligent
Powered by vBadvanced CMPS v3.1.0

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