2

I have a UITableView that is populated using a NSMutableArray . I am using xcode 4.2

the data in the NSMutable array stays in case I switch applications but it gets erased in these two cases : 1-the user switch view and come back .

2-the application is completley closed (i.e. the user double click on the Main button and remove it from the list of running application)

here is the code that I am using

-(NSString *)dataFilePath{

    NSString *dataFilePath;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    dataFilePath = [documentDirectory stringByAppendingPathComponent:@"Test App-Info.plist"];
    return dataFilePath;

}

-(void)saveData{
    [NSKeyedArchiver archiveRootObject:[data copy] toFile:[self dataFilePath]];
}

- (void)loadData
{
    data = [NSKeyedUnarchiver unarchiveObjectWithFile:self.dataFilePath];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  ...

    //saving the history

    NSArray *archivedArray =[NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
    if (archivedArray == nil) {
        data = [[NSMutableArray alloc] init];
    }
    else {
        [self loadData];
        [mainTableView reloadData];
    }
}

Please let me know if I am missing something

Thanks

Edited :

the save data function is loaded in two locations : 1- the app that I am developing scans QR codes , so save data is called in the following function :

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
.... 
   id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results){
        // EXAMPLE: just grab the first barcode
       break;
    }

    if(!symbol) 
        return;

    // EXAMPLE: do something useful with the barcode data
    theBarcodeString = symbol.data;

    //adding the string to the list

    [data addObject:theBarcodeString];
    [self saveData];
    [mainTableView reloadData];
    [self endText];
    stringLabel.text=theBarcodeString;

...
}

it is also called in when the data is edited :

    -(IBAction)editTable{
        UIBarButtonItem *leftItem;
        [mainTableView setEditing:!mainTableView.editing animated:YES];
        if (mainTableView.editing) {
            leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editTable)];
        }
        else {

            leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTable)];

    }

    [self saveData];
    [mainTableView reloadData];

}
3
  • so where from do you save your data? Commented Nov 30, 2011 at 14:30
  • I might be wrong (I am new to App developpment ) but According to the code , the data is saved in dataFilePath in Test App-Info.plist (the project plist) , should this be changed to something else ? Commented Nov 30, 2011 at 14:34
  • i mean in what methods do you call your saveData method? Commented Nov 30, 2011 at 14:37

1 Answer 1

1

You need to look into the following calls:

applicationDidEnterBackground
applicationWillEnterForeground
applicationDidBecomeActive
applicationWillTerminate

They are all methods of UIApplication. Your needs will dictate which of the above store and restore your data depending on when it should happen.

Sign up to request clarification or add additional context in comments.

1 Comment

I changed the name of the file from "Test App-Info.plist" to "text.txt" and I found out that nothing is written in it .... I don t think it is normal , any suggestion ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.