将数据从NSArray保存到Parse

时间:2014-09-07 15:40:55

标签: ios7 parse-platform

我有一些NSObjects。每个元素都具有以下属性(name,id,comment)。我使用parse.com服务器发送和检索我的iOS应用数据。 我保存数据的代码如下

- (IBAction)order:(id)sender {

    PFObject *obj = [PFObject objectWithClassName:@"Table_1"];

    for (SelectedIteam *iteam in _dataArray) {
        [obj setObject:iteam.name forKey:@"Name"];
        [obj setObject:iteam.id forKey:@"ID"];
        [obj setObject:iteam.comment forKey:@"Comment"];
        [obj saveInBackground];
   }

}

但只有我的nsarray的最后一个元素保存在解析服务器中。 如何将所有元素从数组保存到解析服务器。

2 个答案:

答案 0 :(得分:0)

要添加所有元素,必须在for循环中定义PFObject。所以正确的代码将是`

- (IBAction)order:(id)sender {

for (SelectedIteam *iteam in _dataArray) {
    PFObject *obj = [PFObject objectWithClassName:@"Table_1"];
    [obj setObject:iteam.name forKey:@"Name"];
    [obj setObject:iteam.id forKey:@"ID"];
    [obj setObject:iteam.comment forKey:@"Comment"];
    [obj saveInBackground];
    }
}

答案 1 :(得分:0)

我会改用PFObject的saveAllInBackground方法。否则,当你只用一次调用即可实现相同的操作时,你正在为Parse中的每个对象进行一次api调用。

- (IBAction)order:(id)sender {

     NSMutableArray *items = [[NSMutableArray alloc] init];

     for (SelectedIteam *iteam in _dataArray) {

          PFObject *obj = [[PFObject objectWithClassName:@"Table_1"];

          [obj setObject:iteam.name forKey:@"Name"];
          [obj setObject:iteam.id forKey:@"ID"];
          [obj setObject:iteam.comment forKey:@"Comment"];

          [items addObject: obj];
      }

      [PFObject saveAllInBackground:items];
}

https://parse.com/docs/ios/api/Classes/PFObject.html#//api/name/saveAllInBackground