Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:37:52+00:00 2026-05-28T19:37:52+00:00

So I’m trying to save an array when the app terminates so that I

  • 0

So I’m trying to save an array when the app terminates so that I can re-populate the array when I start back up. I’m running into a lot of different errors though, mostly when I try to recreate the array when the app launches. I have taken steps to encode and decode the custom objects, but maybe i’m not doing it correctly… Here is my code:

- (void)applicationWillTerminate:(UIApplication *)application
{
/*
 Called when the application is about to terminate.
 Save data if appropriate.
 See also applicationDidEnterBackground:.
 */

//Here is Where I save the array. "tasks is a pre-populated array of objects

NSData *taskData = [NSKeyedArchiver archivedDataWithRootObject:tasks];
[[NSUserDefaults standardUserDefaults]setObject:taskData forKey:@"tasks"];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

tasks = [[NSMutableArray alloc]init];

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

//NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

if([[NSUserDefaults standardUserDefaults] objectForKey:@"tasks"]){

    NSData *tasksData = [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"];

 //It usually breaks right here

 tasks = [NSKeyedUnarchiver unarchiveObjectWithData:tasksData];

}

Here is the “Task” object that the “tasks” array contains:

Task.h

#import <Foundation/Foundation.h>

@class TeamMember;
@class Project;


@interface Task : NSObject<NSCoding>{

NSDate *endDate;
NSDate *startDate;
NSMutableString* notes;
NSMutableString* taskName;
TeamMember *teamMember;
Project *project;
}
- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder; 

@property  (nonatomic, retain)TeamMember *teamMember;
@property  (nonatomic, retain) Project * project;

@property (nonatomic, retain) NSMutableString *notes;
@property (nonatomic, retain) NSMutableString *taskName;
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic, retain) NSDate *endDate;


@end

Task.m

#import "Task.h"


@implementation Task
@synthesize notes;
@synthesize taskName;
@synthesize endDate;
@synthesize startDate;
@synthesize teamMember;
@synthesize project;


//@synthesize project;
- (void)encodeWithCoder:(NSCoder *)encoder {
//Encode properties, other class variables, etc
[encoder encodeObject:self.notes forKey:@"notes"];
[encoder encodeObject:self.taskName forKey:@"taskName"];
[encoder encodeObject:self.endDate forKey:@"endDate"];
[encoder encodeObject:self.startDate forKey:@"startDate"];

//you need to add in the encoding for teammember and project
}

- (id)initWithCoder:(NSCoder *)decoder {
if((self = [super init])) {
    //decode properties, other class vars
    self.notes = [decoder decodeObjectForKey:@"notes"];
    self.taskName = [decoder decodeObjectForKey:@"taskName"];
    self.endDate = [decoder decodeObjectForKey:@"endDate"];
    self.startDate = [decoder decodeObjectForKey:@"startDate"];

    //decode teamMember and Project here

}
}



@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:37:53+00:00Added an answer on May 28, 2026 at 7:37 pm

    Disregarding the fact that saving such an array to NSUserDefaults instead of using CoreData is poor design, your problem is that you’re not synchronizing the defaults. Add this line [[NSUserDefaults standardUserDefaults] synchronize]; at the bottom of applicationWillTerminate method.

    And please, use CoreData. User defaults aren’t purposed for data persistence, they’re designed to store a few small chunks of data, like bools, or widths and heights of some specific areas which needs to be restored, not custom user content, that actually needs to be persisted.

    Also, do not use applicationWillTerminate method as your resort to saving data, save it right after user adds it. This method does not guarantee that its contents execution will be finished properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.