views:

58

answers:

2

I have a function that sometimes takes too long to run. I want to attempt to run it for 10 seconds, and then just kill it if it's not done. How do I do this in Objective C on the iPhone?

Creating the persistent store coordinator is failing for some users who have a lot of data in their database, because the object model merging takes too long. We've moved that data storage out of the database, but I want to kill the model merging if it takes more than a set time limit, and delete the database file so that the users can continue to use the app.

NSPersistentStoreCoordinator* psc = [[[NSPersistentStoreCoordinator alloc] 
                                        initWithManagedObjectModel: [self managedObjectModelForDatabase:dbName]] autorelease];
A: 

You can use -performSelector:withObject:afterDelay: to schedule a message to be sent in the future.

NSResponder
*I* see what your getting at, but it might require some elaboration.
Williham Totland
Yeah. I know about that. But the function I need to time out is library code, so I can't add a flag check or something in the middle of it. I don't know how to interrupt the thread.
smokey_the_bear
I think we're going to need more info. What precisely are you trying to cancel?
Robot K
I added more info above
smokey_the_bear
+1  A: 

Can you perform your merge in a different thread (don't forget to make a different NSManagedObjectContext for this background thread) and then just have the main thread display a progress message while waiting for an asynchronous finish event from the background migration thread?

I do this in one of my apps with tons of data to avoid the iPhone killing my app during the merge process.

Brent Priddy