Santiago Fabregat
Santiago Fabregat

Reputation: 686

Save every 5 seconds

I have a tableview where I can enter data and save it thanks to core data. I wanted to know what do I have to do so that every 5 seconds my data will "autosave" instead of clicking a save button.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
                                                                    target:self 
                                                                    action:@selector(save)];
}

- (void)save {

    [self setEditing:NO animated:YES];

    for (EditTableCell *cell in [self.tableView visibleCells]) {
        if ([cell isEditable])
            [self.managedObject setValue:[cell value] forKey:[cell key]];
    }

    [self saveManagedObjectContext];
    [self.tableView reloadData];
}

Thanks for the help!

Upvotes: 2

Views: 145

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

As @staticVoidMan mentioned in comments, use a NSTimer to save. However, that frequency is very very fast. I would at least do a call to -[NSManagedObjectContext hasChanges] so that you are not wasting even more cycles.

Upvotes: 2

Related Questions