tiguero
tiguero

Reputation: 11537

Not receiving notification dispatch_group_notify

I am currently using a dispatch_group to get notify when all concurrent tasks are done. I am offloading some heavy tasks on one concurrent queue within the [TWReaderDocument documentFileURL:url withCompletionBlock:] class method.

I have implemented the following code but never received any notification. I don't see what i am potentially doing wrong in the below code:

    dispatch_group_t readingGroup = dispatch_group_create();

    NSFileManager* manager = [NSFileManager defaultManager];

    NSString *docsDir =  [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Data"];

    NSDirectoryEnumerator *dirEnumerator = [manager enumeratorAtURL:[NSURL fileURLWithPath:docsDir]
                                         includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey,
                                                                     NSURLIsDirectoryKey,nil]
                                                            options:NSDirectoryEnumerationSkipsHiddenFiles
                                                       errorHandler:nil];


    // An array to store the all the enumerated file names in
    NSMutableArray *arrayFiles;

    // Enumerate the dirEnumerator results, each value is stored in allURLs
    for (NSURL *url in dirEnumerator) {

        // Retrieve the file name. From NSURLNameKey, cached during the enumeration.
        NSString *fileName;
        [url getResourceValue:&fileName forKey:NSURLNameKey error:NULL];

        // Retrieve whether a directory. From NSURLIsDirectoryKey, also cached during the enumeration.
        NSNumber *isDirectory;
        [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];


        if (![isDirectory boolValue]) {

                dispatch_group_enter(readingGroup);
                TWReaderDocument* doc = [TWReaderDocument documentFileURL:url withCompletionBlock:^(BOOL success) {

                    dispatch_group_leave(readingGroup);

                }];

                [arrayFiles addObject:doc];

        }
        else if ([[[fileName componentsSeparatedByString:@"_" ] objectAtIndex:0] isEqualToString:@"XXXXXX"]) {

            TreeItem* treeItem = [[TreeItem alloc] init];

            arrayFiles = [NSMutableArray arrayWithCapacity:10];

            treeItem.child = arrayFiles;
            treeItem.nodeName = [[fileName componentsSeparatedByString:@"_" ] lastObject];
            [self addItem:treeItem];


        }
    }

    dispatch_group_notify(readingGroup, dispatch_get_main_queue(), ^{ // 4

        NSLog(@"All concurrent tasks completed");

    });

Does the dispatch_group_enter and dispatch_group_leave have to be executed on the same thread?

EDIT The code snippet of my factory method might help aswell:

 + (TWReaderDocument *)documentFileURL:(NSURL *)url withCompletionBlock:(readingCompletionBlock)completionBlock{


            TWReaderDocument * twDoc = [[TWReaderDocument alloc] init];
            twDoc.status = ReaderDocCreated;

            twDoc.doc = [ReaderDocument withDocumentFilePath:[url path] withURL:url withLoadingCompletionBLock:^(BOOL completed) {

                twDoc.status = completed ? ReaderDocReady : ReaderDocFailed;

                completionBlock(completed);

            }];

            return twDoc;

        }

TWReaderDocument is a wrapper class that call internally the following methods of a third-party library (it is a PDF reader)

+ (ReaderDocument *)withDocumentFilePath:(NSString *)filePath withURL:(NSURL*)url withLoadingCompletionBLock:(readingCompletionBlock)completionBlock{

    ReaderDocument *document = [[ReaderDocument alloc] initWithFilePath:filePath withURL:url withLoadingCompletionBLock:[completionBlock copy]];
    return document;
}


- (id)initWithFilePath:(NSString *)fullFilePath withURL:(NSURL*)url withLoadingCompletionBLock:(readingCompletionBlock)completionBlock {
    id object = nil; // ReaderDocument object;

    if ([ReaderDocument isPDF:fullFilePath] == YES) // File must exist
    {
        if ((self = [super init])) // Initialize superclass object first
        {

            _fileName = [ReaderDocument relativeApplicationFilePath:fullFilePath]; // File name

            dispatch_async([ReaderDocument concurrentLoadingQueue], ^{

                self.guid = [ReaderDocument GUID]; // Create a document GUID

                self.password = nil; // Keep copy of any document password

                self.bookmarks = [NSMutableIndexSet indexSet]; // Bookmarked pages index set

                self.pageNumber = [NSNumber numberWithInteger:1]; // Start on page 1

                CFURLRef docURLRef = (__bridge CFURLRef)url;// CFURLRef from NSURL
                self.fileURL = url;

                CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateX(docURLRef, self.password);

                BOOL success;
                if (thePDFDocRef != NULL) // Get the number of pages in the document
                {
                    NSInteger pageCount = CGPDFDocumentGetNumberOfPages(thePDFDocRef);

                    self.pageCount = [NSNumber numberWithInteger:pageCount];

                    CGPDFDocumentRelease(thePDFDocRef); // Cleanup

                    success = YES;
                }
                else // Cupertino, we have a problem with the document
                {
//                    NSAssert(NO, @"CGPDFDocumentRef == NULL");
                    success = NO;
                }


                NSFileManager *fileManager = [NSFileManager new]; // File manager instance

                self.lastOpen = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0]; // Last opened

                NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:fullFilePath error:NULL];

                self.fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date

                self.fileSize = [fileAttributes objectForKey:NSFileSize]; // File size (bytes)

                completionBlock(success);

            });


            //[self saveReaderDocument]; // Save the ReaderDocument object

            object = self; // Return initialized ReaderDocument object
        }
    }

    return object;
}

Upvotes: 0

Views: 280

Answers (1)

ipmcc
ipmcc

Reputation: 29886

It's hard to say what's going on here without knowing more about TWReaderDocument, but I have a suspicion...

First off, no, dispatch_group_enter and dispatch_group_leave do not have to be executed on the same thread. Definitely not.

My best guess based on the info here would be that for some input, [TWReaderDocument documentFileURL:withCompletionBlock:] is returning nil. You might try this instead:

    if (![isDirectory boolValue]) {

            dispatch_group_enter(readingGroup);
            TWReaderDocument* doc = [TWReaderDocument documentFileURL:url withCompletionBlock:^(BOOL success) {

                dispatch_group_leave(readingGroup);

            }];

            // If the doc wasn't created, leave might never be called.
            if (nil == doc) {
                dispatch_group_leave(readingGroup);
            }

            [arrayFiles addObject:doc];

    }

Give that a try.

EDIT: It's exactly as I expected. There are cases in which this factory method will not call the completion. For instance:

if ([ReaderDocument isPDF:fullFilePath] == YES) // File must exist

If -isPDF: returns NO the completionBlock will never be called, and the returned value will be nil.

Incidentally, you should never compare something == YES. (anything non-zero is equivalent to YES, but YES is defined as 1. Just do if ([ReaderDocument isPDF:fullFilePath]). It's equivalent, and safer.

Upvotes: 1

Related Questions