新数据保存到Parse中的Mutable Array而不调用save函数

时间:2014-06-06 00:47:54

标签: ios nsmutablearray parse-platform

我有一个模块,可以根据文本字段中的用户输入保存数据,并将其添加到Parse中保存的数组中。使用预先填充的数据进行测试,模块工作正常。但是,如果我去添加以前没有任何数据的数据,它会导致应用程序一起忽略保存元素。将新数组添加到Parse的最佳方法是什么,以前没有数据存在?这是现有的代码:

- (IBAction)saveButton:(id)sender
{
[getData showGlobalProgressHUDWithTitle:@"Saving Test"];
PFQuery *tankQuery = [PFQuery queryWithClassName:@"WaterTests"];
[tankQuery whereKey:@"tankObjectId" equalTo:_passedValue];

[tankQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error)
    {
        for (PFObject *object in objects)
        {
            calciumArray    =  [object valueForKey:@"calciumArray"];
            nitrateArray    =  [object valueForKey:@"nitrateArray"];
            phosArray       =  [object valueForKey:@"phosphateArray"];
            salinityArray   =  [object valueForKey:@"salinityArray"];
            alkArray        =  [object valueForKey:@"alkArray"];
            phArray         =  [object valueForKey:@"phArray"];
            tempArray       =  [object valueForKey:@"tempArray"];

            [calciumArray   addObject:  addCalcium.text];
            [nitrateArray   addObject:  addNitrate.text];
            [phosArray      addObject:  addPhosphate.text];
            [salinityArray  addObject:  addSalinity.text];
            [alkArray       addObject:  addAlk.text];
            [phArray        addObject:  addPH.text];
            [tempArray      addObject:  addTemp.text];

            [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error)
                {
                    NSLog(@"Something happened");
                    [getData dismissGlobalHUD];
                    [self hideTestView];
                }
                else
                {
                    NSLog(@"Nothing happened: %@", error);
                }
                [self resignFirstResponder];
                [_chartView reloadData];
            }];
        }
    }
}];
}

我知道我已经关闭,但不太明白为什么它没有解雇保存代码。我相信它是因为我试图先找回空的物体,但我可能错了。有人对此有任何建议吗?

UPDATE 这是ViewDidLoad代码,因为它与检索数据有关。

- (void)viewDidLoad
{
    self.title = @"Water Quality";

    statQuery = [PFQuery queryWithClassName:@"WaterTests"];
    [statQuery whereKey:@"tankObjectId" equalTo:_passedValue];
    [statQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
     {
         if (!error)
         {
             for (PFObject *object in objects)
             {
                 [statQuery whereKey:@"tankObjectId" equalTo:_passedValue];
                 calciumArray = [object valueForKey:@"calciumArray"];
                 _footerView.sectionCount = calciumArray.count;
                 NSLog(@"CALCIUM ARRAY: %@", calciumArray);
                 nitrateArray = [object valueForKey:@"nitrateArray"];
                 NSLog(@"NITRATE ARRAY: %@", nitrateArray);
                 phosArray = [object valueForKey:@"phosphateArray"];
                 NSLog(@"PHOSPHATE ARRAY: %@", phosArray);
                 salinityArray = [object valueForKey:@"salinityArray"];
                 NSLog(@"SALINITY ARRAY: %@", salinityArray);
                 alkArray = [object valueForKey:@"alkArray"];
                 NSLog(@"ALKALINITY ARRAY: %@", alkArray);
                 phArray = [object valueForKey:@"phArray"];
                 NSLog(@"PH ARRAY: %@", phArray);
                 tempArray = [object valueForKey:@"tempArray"];
                 NSLog(@"TEMPERATURE ARRAY: %@", tempArray);
             }
         }
         if (calciumArray == nil || [calciumArray count] == 0)
         {
             NSLog(@"You should probably fire off the new test function here.");
             [_chartView setUserInteractionEnabled:NO];
             [self newTest:self];
             NSLog(@"Error: %@", error);
         }
     }];

这是视图中唯一提及数据的时间。我在其他地方分配和初始化阵列,但我不认为考虑到现有阵列拉动并保存得很好的问题。

2 个答案:

答案 0 :(得分:0)

(PFObject *object in objects) 
{ 
  calciumArray = [object valueForKey:@"calciumArray"]; 
  . 
  . 
  . //Other object for array

  [arrayMu] [calciumArray addObject: addCalcium.text]; //Add in NSMutableArray
  .
  .
  . 
  [objects save]; //Instead of saveinbackground.
} 

试一试。也请及时回复,因为经过很长一段时间后,我必须再次阅读您的所有查询,而不是与之联系。

... UPADTE

-(IBAction)updateButton:(id)sender
{
 PFQuery *query = [PFQuery queryWithClassName:@"UserDetail"];

 NSString *str =self.nameTextField.text;

 [query whereKey:@"name" containsString:str];

 [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)
 {
     if (!error)
     {
         NSLog(@"Successfully retrieved: %@", [object objectId]); //I retrieve the object succesfully 

         object[@"job"] = self.jobTextField.text;   //Made changes to it.
         object[@"hobby"] = self.hobbyTextField.text;

         [object saveInBackground];  //Again saved it in background
     }
     else
     {
         NSLog(@"Error: %@", [error localizedDescription]);
     } }];

}

然后我检查了我的解析数据库,我的数据被修改了。

答案 1 :(得分:0)

如果没有其他可用的东西可以通过向Parse明显添加新对象,我能够从单个对象中正确添加一个新数组:

- (IBAction)saveButton:(id)sender
{
    [getData showGlobalProgressHUDWithTitle:@"Saving Test"];
    PFQuery *tankQuery = [PFQuery queryWithClassName:@"WaterTests"];
    [tankQuery whereKey:@"tankObjectId" equalTo:_passedValue];
    PFObject *testObject = [[PFObject alloc] initWithClassName:@"WaterTests"];

    [tankQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count == 0)
        {
            [testObject setObject:_passedValue forKey:@"tankObjectId"];
            [testObject addObject:addCalcium.text forKey:@"calciumArray"];
            [testObject addObject:addNitrate.text forKey:@"nitrateArray"];
            [testObject addObject:addPhosphate.text forKey:@"phosphateArray"];
            [testObject addObject:addSalinity.text forKey:@"salinityArray"];
            [testObject addObject:addAlk.text forKey:@"alkArray"];
            [testObject addObject:addPH.text forKey:@"phArray"];
            [testObject addObject:addTemp.text forKey:@"tempArray"];

            [testObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error)
                {
                    [getData dismissGlobalHUD];
                    [self hideTestView];
                    NSLog(@"Something, somewhere, saved.");
                }
                else
                {
                    NSLog(@"HOORAY ERROR: %@", error);
                }
                [_chartView reloadData];
            }];
        }

但是,我还必须检查是否有任何对象开始。如果没有,只需将它们添加到新的中即可。如果有的话,我必须运行代码来查询数据库,并为使用给定条件找到的每个对象添加for循环:

        else if (objects.count != 0)
        {
            for (PFObject *object in objects)
            {
                [object addObject:addCalcium.text forKey:@"calciumArray"];
                [object addObject:addNitrate.text forKey:@"nitrateArray"];
                [object addObject:addPhosphate.text forKey:@"phosphateArray"];
                [object addObject:addSalinity.text forKey:@"salinityArray"];
                [object addObject:addAlk.text forKey:@"alkArray"];
                [object addObject:addPH.text forKey:@"phArray"];
                [object addObject:addTemp.text forKey:@"tempArray"];

                [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
                {
                    if (!error)
                    {
                        [getData dismissGlobalHUD];
                        [self hideTestView];
                        [_chartView reloadData];
                        NSLog(@"Here we go again");
                    }
                }];
            }
        }
    }];
}

我将此作为我自己的答案提交给社区维基,以帮助其他人解决类似的问题。问题:已解决!