0

Overview

I have an iOS project in which I store a list of names in a table view.

I understand the moment the database is opened / created, there is a completion handler to execute a method.

Steps:

  1. In my case, the first screen is a table view which displays the names
  2. there is a + button (on the navigation bar) to add a new name which would take it to a new screen to enter the details.

Concern

My worry is that if I fetch the names for the tableview in the completion handler and if the user doesn't wait for the database to be opened and creates a new name, while saving it, the database might not be opened / created.

Questions

  1. How to handle such a situation ?
  2. Am I missing something ?
user1046037
  • 16,755
  • 12
  • 92
  • 138
  • Are you fetching the contact list asynchroniously?Ad,what do you mean by database open/created?Core Data database must be already created,if you fetch smth,and you really don't need to care about whether it is"opened" or not. – Nikita Pestrov Apr 14 '12 at 04:08
  • thanks, if the database file exists, then I would be opening it using UIManagedDocument's method openWithCompletionHandler:... and if it is not open I would create a new DB file using the method saveToURL. Since the completion handler is executed asynchronously, there is a chance user might be able to add data without the database being open – user1046037 Apr 14 '12 at 04:19
  • You'd better make him wait a bit,until everything is created.Add an fading UIButton in the complition handler – Nikita Pestrov Apr 14 '12 at 04:47

2 Answers2

2

Really, you should not be allowing the user to change stuff until everything is initialized. If it takes a while, then just disable that functionality until ready.

In this case it's easy. Start with the "add" button disabled, and enable it in the completion handler.

You can write a documentIsReady method (which will enable editing, and anything else you want to do when "all is ready."), and then call it when the document is ready to be used. See code below for example.

    if (![[NSFileManager defaultManager] fileExistsAtPath:[doc.fileURL path]]) {
        [doc saveToURL:doc.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            [self documentIsReady: success];
        }];
    } else if (doc.documentState == UIDocumentStateClosed) {
        [doc openWithCompletionHandler:^(BOOL success) {
            [self documentIsReady: success];
        }];
    } else {
        BOOL success = doc.documentState == UIDocumentStateNormal;
        [self documentIsReady: success];
    }
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • thanks, is it done using UIDocumentStateChangedNotification (adding an observer and passing selector to be executed) ? – user1046037 Apr 14 '12 at 05:05
  • No, you do it in the completion handler. I'll edit the answer with an pseudo-example. – Jody Hagins Apr 14 '12 at 06:07
  • awesome !!! thanks a ton Jody,was very helpful and that code is clean and is less complicated than what I had in mind !! thanks again – user1046037 Apr 14 '12 at 10:20
1

Well, one (simple) way of handling this situation as add a timer to wait until the database is opened / created. You could also grey the button (the + button) out until the database is ready.

Allison
  • 2,213
  • 4
  • 32
  • 56
  • thanks, i have a doubt, does adding a NSTimer have to be in a separate thread or can it be in the same thread because the completion handler is executed asynchronously anyway ? – user1046037 Apr 14 '12 at 03:57
  • NStimers run in a sperate thread all by them selfs. That's how you can run 2 timers at once – Allison Apr 14 '12 at 13:25