I am trying to open several UIDocument
instances in a loop, using - (void)openWithCompletionHandler:(void (^)(BOOL success))completionHandler
.
To be precise, I am loading a file list from the document folder, and each file representing a user which can log in into my app. Now, obviously I want to show my login dialog after all the users / UIDocument
were opened. Unfortunately I can't seem to come up with a good approach for this. What I am having is:
-(void)reloadUsers
{
__block int cnt= 0;
NSArray* localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[AppDelegate localDocumentsDirectoryURL] path] error:nil];
for (NSString* document in localDocuments)
{
if (![document hasSuffix:@".user"])
continue;
// User beeing UIDocument subclass
User* user=[[[User alloc] initWithFileURL:[NSURL fileURLWithPath:[[[AppDelegate localDocumentsDirectoryURL] path]
stringByAppendingPathComponent:document]]] autorelease];
NSLog(@"Opening user document at url '%@'/'%@' ...", [AppDelegate localDocumentsDirectoryURL], document);
[user openWithCompletionHandler:^(BOOL success)
{
[self.userList addObject:user];
cnt++;
}];
}
NSLog(@"Loaded %d local users", cnt);
}
Question 1: will the NSLog at the end even work and show the actual number of users loaded, or will it be a random value since the cnt++
is done asynchroneously?
Question 2: How can I change the above function so that I can safely do the following:
{ // app finished loading
[MyClass reloadUsers];
[LoginDialog show]; // this shall happen AFTER reloadUsers has loaded ALL documents
}