4

I have this function, but when I run:

theRecipes.append(theRecipe);

...the size of the array 'theRecipes' is exactly the same. I share you the code so you can overview what I'm trying to do on Swift Language.

func retrieveAll() -> [Recipe] {
    var query = PFQuery(className: RECIPE_PARSE_CLASSNAME);
    var theRecipes: [Recipe] = [Recipe]();

    query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]!, error: NSError!) -> Void in

        if (error == nil) {
            // The find succeeded.
            println("Successfully retrieved \(objects.count) recipes.");

            // Do something with the found objects
            if let recipes = objects as? [PFObject] {
                for recipe in recipes {
                    let theRecipe: Recipe = Recipe();
                    theRecipe.name = recipe[RECIPE_PARSE_NAME_NAME] as String;
                    theRecipe.about = recipe[RECIPE_PARSE_ABOUT_NAME] as String;
                    theRecipe.cover = recipe[RECIPE_PARSE_COVER_NAME] as String;
                    theRecipe.preview = recipe[RECIPE_PARSE_PREVIEW_NAME] as String;

                    println(theRecipe.name);
                    theRecipes.append(theRecipe);
                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error) \(error.userInfo!)");
            Bugsnag.notify(nil, withData: ["data": error.description]);
        }
    };

    return theRecipes;
}

What I'm trying to do is collect every PFObject to translate it to my own Model class and for managing it that way while in memory. So I translate it and append it to the array to deliver it fulfilled with recipes.

I want to make clear that I already checked that I'm successfully retrieving every object from Parse and they're not null.

6
  • 3
    You can't return the array like this from a method that uses an asynchronous method to fetch the data. The return statement will run immediately, before the block in findObjectsInBackgroundWithBlock is executed. Commented Mar 31, 2015 at 0:23
  • You're completely right. I'll fix it right now. But even so, is it normal that when I'm debugging a line after the append the array has still 0 elements? Commented Mar 31, 2015 at 0:32
  • 1
    Did you do that in the debugger or with a log? If you log theRecipes in the line right after you append it, it should show the correct number of elements. Commented Mar 31, 2015 at 1:05
  • How come Parse "fetchInBackground" questions are becoming as prevalent as Java string comparison with == questions? Commented Mar 31, 2015 at 1:52
  • @rdelmar What would be a solution instead? I'm having the same issue where I need to return an array from an asynchronous method. Commented Aug 12, 2015 at 8:00

1 Answer 1

2

This was my solution:

func retrieveAll(returner: (Array<Recipe>) -> Void) {
    var query = PFQuery(className: RECIPE_PARSE_CLASSNAME);
    var theRecipes: Array<Recipe> = Array<Recipe>();

    query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]!, error: NSError!) -> Void in

        if (error == nil) {
            // The find succeeded.
            println("Successfully retrieved \(objects.count) recipes.");

            // Do something with the found objects
            if let recipes = objects as? [PFObject] {
                for recipe in recipes {
                    let theRecipe: Recipe = Recipe();
                    theRecipe.name = recipe[RECIPE_PARSE_NAME_NAME] as String;
                    theRecipe.about = recipe[RECIPE_PARSE_ABOUT_NAME] as String;
                    theRecipe.cover = recipe[RECIPE_PARSE_COVER_NAME] as String;
                    theRecipe.preview = recipe[RECIPE_PARSE_PREVIEW_NAME] as String;

                    println(theRecipe.name);
                    theRecipes.append(theRecipe);
                }

                returner(theRecipes);
            } else {
                println();
                returner(Array<Recipe>());
            }
        } else {
            // Log details of the failure
            println("Error: \(error) \(error.userInfo!)");
            Bugsnag.notify(nil, withData: ["data": error.description]);
            returner(Array<Recipe>());
        }
    };
}

Using a returner method to deliver my mapped recipes asynchronously.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.