【问题标题】:How to save data from parse to a NSArray iOS如何将解析中的数据保存到 NSArray iOS
【发布时间】:2014-01-31 09:42:27
【问题描述】:

如何以以下格式将解析中的数据保存到NSArray。用于填充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];

我尝试了下面的代码,但是 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);

有没有办法将数据保存在上述类型的数组中?

【问题讨论】:

  • 您是否NSLog() NSArray *objects 以确保您确实收到了所需的对象?
  • 尝试在findObjectsInBackgroundWithBlock:的块内登录
  • 它有效,虽然它在块中,但它仍然没有显示在上面的数组中
  • @mugunthan 你的NSLog(@"Array : %@", arrayName); 将在findObjectsInBackgroundWithBlock 完成之前工作
  • _candyArray 输出为“”、“”、“”、“”、“ ", "", "", "", "", "" 而来自 parse.com 的输出“<0gc8p3abia:>

标签: ios objective-c nsarray parse-platform pfquery


【解决方案1】:

你的糖果是从哪里来的?如果是自定义类,那么很可能一开始就不会保存解析。

这是使用现有数据的一种方法,但也许最好创建一个 PFObject 子类。 (检查文档)

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.

好的,下一步是检索它们。 (基本上是 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]);
    }
}];

然后,如果您想按照您最初的问题将它们保存回数组中

【讨论】:

    【解决方案2】:

    它为空的原因是

    findObjectsInBackgroundWithBlock

    运行该日志语句时未完成。如果要处理结果(或记录),则需要将 log 语句放在块内,也可以使用

    findObjectsInBackgroundWithTarget:selector:

    而是在你的回调方法中包含处理代码。

    【讨论】:

      【解决方案3】:

      我认为您尚未分配数组,因此请按如下方式更改您的代码

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-12
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多