0

I'm converting my ViewController from Objective-C to Swift.

I was wondering if findObjectsInBackgroundWithBlock is the smartest thing to still be using (or if that will load too slow), and if I should be using it the same way.

I'm using it to get my Parse data and save it to my App Group and/or NSUserDefaults.

    // Query Parse
    PFQuery *query = [PFQuery queryWithClassName:@"data"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {

            NSMutableArray *localMatchup = [@[] mutableCopy];

            for (PFObject *object in objects) {


                // Add objects to local Arrays
                [localMatchup addObject:[object objectForKey:@"matchup"]];

                // App Group
                NSString *container = @"group.com.ramsden.playoffs";
                NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

                // Matchup
                [defaults setObject:localMatchup forKey:@"KeyMatchup"];
                NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
                self.matchupArray = localMatchup;

Update: I tried Lamar's answer below, but am getting an error on the findObjectsInBackgroundWithBlock line that says: '([AnyObject]!, NSError!) -> Void' is not compatible to 'PFArrayResultBlock?

enter image description here Here's a closer look at the specific line: enter image description here

1 Answer 1

1

Try this:

func GetBackFromParse(localMatchUp:NSMutableArray){
var query = PFQuery(className: "data")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in

    if error == nil
    {

        if let objects = objects as? [PFObject]{

            for SingleObject in objects
            {

                var matchup = SingleObject["matchup"]
                localMatchUp.addObject(matchup!)

                 // do whatever you want to do
            }

        }
    }

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

9 Comments

Hey @Lamar, thanks for the response! I think what you're doing makes sense to me, the only thing so far is that I'm getting an error on the findObjectsInBackgroundWithBlock line that says: '([AnyObject]!, NSError!) -> Void' is not compatible to 'PFArrayResultBlock?`. Any idea?
replace with what i have on the code query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
Yup, that works. And for the part where you put // do whatever you want to do, is that corresponding to the parts of my code where I have // App Group and Matchup?
yeah where you had to create the instance of NSUserDefaults
Gotcha. Seems like I'm close, but I'm getting a error on localMatchUp.addObject(matchup!) line "Use of unresolved identifier localMatchup". I'm assuming because in my ObjC I specified that mutable array, but in the Swift I'm not seeing that its done that? Am I on the right track there, or what do you think?
|

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.