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 10-24-2010, 08:22 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 38
loddy1234 is on a distinguished road
Lightbulb NSMutableArray

I have some data stored in a NSMutableArray as NSNumbers. how can i save the mutable array? It is declared in the app delegate so I know I can use the
Code:
- (void)applicationWillTerminate:(UIApplication *)application {
  }
to save it, and I can use the
Code:
- (void)viewDidLoad {
}
to load it but what code do I use in the applicationWillTerminate method to save it?

Last edited by loddy1234; 10-24-2010 at 08:24 AM.
loddy1234 is offline   Reply With Quote
Old 10-24-2010, 08:59 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by loddy1234 View Post
I have some data stored in a NSMutableArray as NSNumbers. how can i save the mutable array? It is declared in the app delegate so I know I can use the
Code:
- (void)applicationWillTerminate:(UIApplication *)application {
  }
to save it, and I can use the
Code:
- (void)viewDidLoad {
}
to load it but what code do I use in the applicationWillTerminate method to save it?
I would advise against saving in applicationWillTerminate. That method doesn't get called under OS 4.x

I would suggest saving your array every time it changes.

NSUserDefaults would work quite nicely.

Code:
#define K_ARRAY_KEY @"array_key"
Define numberArray as a retained property of the object that needs to use it, or your app delegate,
if you need access to it throughout your app.

Code:
- (void) saveData
{
  NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  [defaults setObject: self.numberArray forKey: K_ARRAY_KEY];
  [defaults synchronize];
}
//Add this code to your applicationDidFinishLaunching/application:didFinishLaunchingWithOptions:
//method (or viewDidLoad, if you just need the array in a view controller)

Code:
  NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  NSArray* tempArray = [defaults objectForKey: K_ARRAY_KEY];

  //the object comes back from user defaults as an NSArray, so copy it to a mutable array
  self.numberArray = [NSMutableArray arrayWithCapacity: [tempArray count]];
  [self.numberArray setArray: tempArray];

Note that saving a container object like an array to user defaults only works if every object in the array is a "plist" object (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary).
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-24-2010, 01:18 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

applicationWillTerminate can indeed be called from iOS 4 if you have not marked your info.plist to enable background running. But if you have enabled background running for iOS 4, then Duncan is right that it will not be called.

You could save the array every time it changes. But depending on how your array is used in your application, that could run into performance problems.

I have found it quite safe to save state in both applicationWillTerminate and applicationDidEnterBackground (using a third method that I call from both of these). As long as your app or the iPhone does not crash, you can count on on or the other of these methods to be called. And doing things those way will allow you to target iOS 3.x as well.
RLScott is offline   Reply With Quote
Old 10-24-2010, 01:36 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RLScott View Post
applicationWillTerminate can indeed be called from iOS 4 if you have not marked your info.plist to enable background running. But if you have enabled background running for iOS 4, then Duncan is right that it will not be called.

You could save the array every time it changes. But depending on how your array is used in your application, that could run into performance problems.

I have found it quite safe to save state in both applicationWillTerminate and applicationDidEnterBackground (using a third method that I call from both of these). As long as your app or the iPhone does not crash, you can count on on or the other of these methods to be called. And doing things those way will allow you to target iOS 3.x as well.

All true.

I generally save changes to user defaults as soon as I make them, except in a loop. In that case, I wait until a set of changes is complete, and then save updates. Obviously you need to test performance and adjust accordingly.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
integer, load, nsmutablearray, number, save

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: 323
12 members and 311 guests
chiataytuday, condor304, Desert Diva, Domele, dre, dreamdash3, mottdog, oceanlablight, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,659
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dreamdash3
Powered by vBadvanced CMPS v3.1.0

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