简体   繁体   中英

saving NSMutable Array Data

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 (ie 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];

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM