0
votes

Im downloading data from web and inserting the downloaded data into sqlite database. Now what i want is the data which is inserting into the database that should happen in background while downloading also the user can do other things on the view. So is that possible u isert data into sqlite in the background process.

im using this but the application crashes on using this.

NSNumber *number = [NSNumber numberWithInteger:1];
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                        selector:@selector(InsertIntodatabase)
                                                                          object:number];

[queue addOperation:operation];
[operation release];

Thanks in advance

1

1 Answers

0
votes

You can insert data in the background thread. However, if you simultaneously (more or less) attempt to access the DB from two different threads you'll get a "database locked" error which is hard to recover from.

So you need to provide some external interlock to prevent simultaneous access from two different threads. In some cases the program logic itself will prevent this, and in other cases you need to implement an explicit lock mechanism.