Bazinga
Bazinga

Reputation: 2466

saving in NSUserDefaults

I'm having a problem on how/where I can save my NSUserDefaults for applicationDidEnterBackground and applicationWillTerminate, I need to save my NSArray count in NSUserDefaults from another UIViewController when user exits the app or enter background. But I don't know if I would save the right integer, because my NSArray count is being updated when I am deleting on it on UIView. I cant save it on viewDidLoad and viewDidAppear. Hope someone would help. Thankyou.

Upvotes: 1

Views: 1302

Answers (4)

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

If u need to save from another ViewController then its better u add NSMutableArray in appDelegate. Whenever you delete or add object then save the count of array as it will overwrite like this:

 [[NSUserDefaults standardUserDefaults] setInteger:[yourArray count] forKey:@"Count"];
 [[NSUserDefaults standardUserDefaults] synchronize];

Retrieve like this:

 NSInteger count = [[NSUserDefaults standardUserDefaults] integerForKey:@"Count"];

EDIT : synchronize : Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.

Refer NSUserDefaults_Class as clearly states gets called perodically and we don't have to wait for their call

Upvotes: 1

rptwsthi
rptwsthi

Reputation: 10182

For details on using NSUserDefault check this

  • [[NSUserDefaults standardUserDefaults] setBool:<#(BOOL)#> forKey:<#(NSString *)#>], this Is for Boolean you an add setInt/setFloat/SetDouble etc and even ObjectiveC objects with setValue/setObject.

    [[NSUserDefaults standardUserDefaults] setValue:<#(id)#> forKey:<#(NSString *)#>];
    [[NSUserDefaults standardUserDefaults] setObject:<#(id)#> forKey:<#(NSString *)#>];
    
  • To retrieve the value you can use getters like boolForKey ([[NSUserDefaults standardUserDefaults] boolForKey:<#(NSString *)#>]) intForKey etc..

  • Use removeObjectForKey to remove objects.

Upvotes: 0

Rajneesh071
Rajneesh071

Reputation: 31091

You can use this code to save value to NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:@"YourKey"]
[defaults synchronize];

Upvotes: 0

AppleDelegate
AppleDelegate

Reputation: 4249

Create the required NSMutableArray in your appDelegate class i.e create a property of NSMutableArray iVar,then later you can update ,add ,delete contents from it in any of the classes.Thus you shall get the array count in the applicationDidEnterBackground and applicationWillTerminate notification functions in the appDelegate function itself and the array count should be accurate..

in yourAppDelegate.h

create ..

    @interface yourAppDelegate : UIResponder <UIApplicationDelegate>{
    NSMutableArray *myArray;
    }
@property(nonatomic,strong)NSMutableArray *myArray; //replace strong by retain if ios <5

in yourAppDelegate.m

@synthesize myArray;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *
)launchOptions{
     self.myArray = [NSMutableArray array];
}

further in your required functions in appDelegate.m file

- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardDefaults]setValue:[NSNumber numberWithInt:[self.myArray count] forKey:@"myOwnKey"]];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

in case if you want to add objects to this array in any other class just create a delegate of the yourAppDelegate .

Eg in viewDidLoad of class A,

-(void)viewDidLoad{  // assuming viewController A

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplicatio sharedApplication]delegate];
[appDelegate.myArray addObject:@"1232"];
}

Upvotes: 0

Related Questions