Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:01:23+00:00 2026-06-05T09:01:23+00:00

I have the following method that uses blocks and completion handlers: // Returns YES

  • 0

I have the following method that uses blocks and completion handlers:

    // Returns YES if photo is stored in a virtual vacation.
- (BOOL) photoIsOnVacation
{
    __block BOOL photoOnFile = NO;

    // Identify the documents folder URL.
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSError *errorForURLs      = nil;
    NSURL *documentsURL        = [fileManager URLForDirectory:NSDocumentDirectory
                                                     inDomain:NSUserDomainMask
                                            appropriateForURL:nil
                                                       create:NO
                                                        error:&errorForURLs];
    if (documentsURL == nil) {
        NSLog(@"Could not access documents directory\n%@", [errorForURLs localizedDescription]);
    } else {

        // Retrieve the vacation stores on file.
        NSArray *keys = [NSArray arrayWithObjects:NSURLLocalizedNameKey, nil];
        NSArray *vacationURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:documentsURL
                                                              includingPropertiesForKeys:keys
                                                                                 options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                                   error:nil];
        if (!vacationURLs) photoOnFile = NO;
        else {

            // Search each virtual vacation for the photo.
            for (NSURL *vacationURL in vacationURLs) {
                NSError *errorForName  = nil;
                NSString *vacationName = nil;
                [vacationURL getResourceValue:&vacationName forKey:NSURLNameKey error:&errorForName];
                [VacationHelper openVacationWithName:vacationName usingBlock:^(UIManagedDocument *vacationDocument) {
                    NSError *error              = nil;
                    NSManagedObjectContext *moc = vacationDocument.managedObjectContext;

                    // Build fetch request.
                    NSFetchRequest *request          = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
                    NSString *currentPhotoID         = [self.chosenPhoto objectForKey:FLICKR_PHOTO_ID];
                    request.predicate                = [NSPredicate predicateWithFormat:@"unique = %@", currentPhotoID];
                    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"unique" ascending:YES];
                    request.sortDescriptors          = [NSArray arrayWithObject:sortDescriptor];

                    // Execute fetch request.
                    NSArray *checkPhotos = [moc executeFetchRequest:request error:&error];
                    if (error) {
                        NSLog(@"Error searching for photo:%@",error);
                    } else {
                        Photo *checkPhoto = [checkPhotos lastObject];
                        if ([checkPhoto.unique isEqualToString:currentPhotoID]) photoOnFile = YES;
                    }
                }];
                if (photoOnFile) break;
            }
        }
    }
    return photoOnFile;
}

My problem is that photoOnFile is always false because execution reaches the return before the block that contains the fetch request. I’ve tried embedding the photoOnFile assignment within dispatch_async(dispatch_get_main_queue(),^{ but that hasn’t helped. Any guidance appreciated.

Update: here is the reworked code successfully incorporating Ken’s recommended solution:

    - (void)checkIfPhotoIsOnVacationAndDo:(void(^)(BOOL photoIsOnVacation))completionBlock
{
    __block BOOL photoIsOnVacation = NO;

    // Identify the documents folder URL.
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSError *errorForURLs      = nil;
    NSURL *documentsURL        = [fileManager URLForDirectory:NSDocumentDirectory
                                                     inDomain:NSUserDomainMask
                                            appropriateForURL:nil
                                                       create:NO
                                                        error:&errorForURLs];
    if (documentsURL == nil) {
        NSLog(@"Could not access documents directory\n%@", [errorForURLs localizedDescription]);
    } else {

        // Retrieve the vacation stores on file.
        NSArray *keys = [NSArray arrayWithObjects:NSURLLocalizedNameKey, nil];
        NSArray *vacationURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:documentsURL
                                                              includingPropertiesForKeys:keys
                                                                                 options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                                   error:nil];
        if (!vacationURLs) photoIsOnVacation = NO;
        else {

            // Search each virtual vacation for the photo.
            for (NSURL *vacationURL in vacationURLs) {
                NSError *errorForName  = nil;
                NSString *vacationName = nil;
                [vacationURL getResourceValue:&vacationName forKey:NSURLNameKey error:&errorForName];
                [VacationHelper openVacationWithName:vacationName usingBlock:^(UIManagedDocument *vacationDocument) {
                    NSError *error              = nil;
                    NSManagedObjectContext *moc = vacationDocument.managedObjectContext;

                    // Build fetch request.
                    NSFetchRequest *request          = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
                    NSString *currentPhotoID         = [self.chosenPhoto objectForKey:FLICKR_PHOTO_ID];
                    request.predicate                = [NSPredicate predicateWithFormat:@"unique = %@", currentPhotoID];
                    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"unique" ascending:YES];
                    request.sortDescriptors          = [NSArray arrayWithObject:sortDescriptor];

                    // Execute fetch request.
                    NSArray *checkPhotos = [moc executeFetchRequest:request error:&error];
                    if (error) {
                        NSLog(@"Error searching for photo:%@",error);
                    } else {
                        Photo *checkPhoto = [checkPhotos lastObject];
                        if ([checkPhoto.unique isEqualToString:currentPhotoID]) {
                            photoIsOnVacation = YES;
                            completionBlock(photoIsOnVacation);
                        }
                    }
                }];
                if (photoIsOnVacation) break;
            }
            completionBlock(photoIsOnVacation);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:01:25+00:00Added an answer on June 5, 2026 at 9:01 am

    Asynchronicity tends to spread. Once you make an API asynchronous, all of its callers have to be redesigned to work asynchronously, too. Therefore, a method like your - (BOOL) photoIsOnVacation is untenable because its interface is synchronous – the caller expects to have an answer as soon as the call completes – but the implementation doesn’t work that way.

    You have to redesign to something like - (void) checkIfPhotoIsOnVacationAndDo:(void(^)(BOOL photoIsOnVacation))block. That takes a block from the caller and invokes the block with the answer when it is known.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method that is replacing a pound sign from the file
I have the following method that is supposed to be a generic Save to
I have a method that contains the following (Java) code: doSomeThings(); doSomeOtherThings(); doSomeThings() creates
I have a method that I will use in the following contexts: 1. User
I have a program that makes use of the following method to get a
I have an interface (called Subject ) that has the following method: public void
I have a simple mapview that has the following viewdidload method and didupdate to
I have dynamic array in struct and a method that uses the dynamic array.
I have a method that uses an IList<T> as a parameter. I need to
I have the following code in specman that I inherited: some_method() is { var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.