简体   繁体   English

如何将数据从解析保存到NSArray iOS

[英]How to save data from parse to a NSArray iOS

How can I save data from parse to an NSArray in the below format. 如何以以下格式将数据从解析保存到NSArray which is being used to populate a UITableView , which is to be used for a search bar. 它用于填充UITableView ,该UITableView将用于搜索栏。

_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 我尝试了下面的代码,但是id没有加载任何数据,当我通过NSLog运行它时,它始终将值返回为null

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? 是否有将数据保存在上述类型的数组中的方法?

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. 这是使用现有数据的一种方法,但是创建PFObject子类也许更好。 (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) (基本上是rajjjjj的示例,有一些更改)

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 然后,如果您想将它们保存回数组中,如您最初提出的问题

The reason it is null is that the 之所以为null的原因是

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 如果要处理结果(或记录结果),则需要将log语句放入块中,或者可以使用

findObjectsInBackgroundWithTarget:selector:

instead, and have the handling code in your callback method. 而是在您的回调方法中包含处理代码。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM