How to save data from parse to a NSArray iOS

2019-06-13 23:49发布

问题:

How can I save data from parse to an NSArray in the below format. which is being used to populate a UITableView, which is to be used for a search bar.

_candyArray = [NSArray arrayWithObjects:
                  [Candy candyOfCategory:@"chocolate" name:@"chocolate bar" mName:@"test1"],
                  [Candy candyOfCategory:@"chocolate" name:@"chocolate chip" mName:@"test1"],
                  [Candy candyOfCategory:@"chocolate" name:@"dark chocolate" mName:@"test1"],
                  [Candy candyOfCategory:@"hard" name:@"lollipop" mName:@"test1"],
                  [Candy candyOfCategory:@"hard" name:@"candy cane" mName:@"test1"],
                  [Candy candyOfCategory:@"hard" name:@"jaw breaker" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"caramel" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"sour chew" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"peanut butter cup" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"gummi bear" mName:@"test1"], nil];

I tried the below code, but id didn't load any data, keeps returning the value as null when I run it through NSLog

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"Name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        {
            self.arrayName = objects;
        }   
    }else {
        NSLog(@"Error, %@ %@",error,[error userInfo]);
    }
}];

NSLog(@"Array : %@", arrayName);

Is there anyway to save the data in the above type of array?

回答1:

Where are your candy objects coming from? If it is a custom class, then it is likely not saving to parse in the first place.

Here's one way to use your existing data, but perhaps it's better to create a PFObject subclass. (Check in the docs for that)

NSMutableArray * candyObjects = [NSMutableArray alloc]init];

for (Candy * candy in _candyArray) {   
    PFObject * newCandy = [PFObject objectWithClassName:@"Candy"];
    newCandy[@"category"] = candy.category; // or however you store your candy object's properties
    newCandy[@"name"] = candy.name; // I prefer lowercase names, I notice you're querying uppercase
    newCandy[@"mName"] = candy.mName;

    [candyObjects addObject:newCandy];
}

[PFObject saveAll:candyObjects]; // should run in background, but for now just showing to save.

Ok, next step is retrieving them. (basically rajjjjj's example w/ a few changes)

PFQuery *query = [PFQuery queryWithClassName:@"Candy"];
[query orderByAscending:@"name"]; // lowercase to match above
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"retrievedObjects:%@", objects);

        // Now to get them back into an array like you wanted before
        // again, perhaps you could skip this by subclassing your candy ob

        for (PFObject * obbie in objects) {
            // mutableArrayRef represents wherever you'd like your candies added.
            // make sure it has memory allocated, and it has been initialized
            [mutableArrayRef addObject:[Candy candyOfCategory:obbie[@"category"] name:obbie[@"name"] mName:obbie[@"mName"]]];
        }

        // now continue in whatever way you please, all of your candy should be in mutableArrayRef
        // if you're doing multiple searches, make sure you clear the array before adding
        // otherwise you will stack the data and get undesired results
    }
    else {
        NSLog(@"Error, %@ %@",error,[error userInfo]);
    }
}];

Then, if you want to save them back into an array as your question originally stated



回答2:

The reason it is null is that the

findObjectsInBackgroundWithBlock

is not finished when that log statement is run. If you want to handle the results (or log them), you need to put the log statement inside the block, or you can use

findObjectsInBackgroundWithTarget:selector:

instead, and have the handling code in your callback method.



回答3:

You have not allocated the array i think so change your code as follows

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"Name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
    {
        self.arrayName = [[NSArray alloc]initwithArray:objects];
    }   
}else {
    NSLog(@"Error, %@ %@",error,[error userInfo]);
}
}];

NSLog(@"Array : %@", self.arrayName);