0

I am trying to save a NSMutableArray to an array associated with the currently logged in user on my Parse User table but my array doesn't get saved. What am I doing wrong?

This method is called in viewDidLoad()

- (void)getSavedLocations {
    if ([[PFUser currentUser]objectForKey:@"Latitude"] == NULL) {
        self.latitudeArray = [[NSMutableArray alloc]init];
        self.longitudeArray = [[NSMutableArray alloc]init];
    }
    else {
        self.latitudeArray = [[PFUser currentUser]objectForKey:@"Latitude"];
        self.longitudeArray = [[PFUser currentUser]objectForKey:@"Longitude"];
    }
}

This method is adds objects to my arrays but the Parse arrays don't get updated with the new data.

- (IBAction)saveLocationPressed:(id)sender {
    NSString *latString = [NSString stringWithFormat:@"%f", self.myLatitude];
    NSString *longString = [NSString stringWithFormat:@"%f", self.myLongitude];

    [self.latitudeArray addObject:latString];
    [self.longitudeArray addObject:longString];

    [[PFUser currentUser]setObject:self.latitudeArray forKey:@"Latitude"];
    [[PFUser currentUser]setObject:self.longitudeArray forKey:@"Longitude"];
}

2 Answers 2

1

I figured it out. I had to add [[PFUser currentUser]saveInBackground];

Sign up to request clarification or add additional context in comments.

3 Comments

could you tell some details about what's wrong with your code?could you give the right code ?
There was nothing wrong other than that I needed to add saveInBackground
YES You had to add The SaveInBackground Function
0

First check if self.latitudeArray is NULL in the method saveLocationPressed: , if you first save ,then get,you self.latitudeArray may be NULL. suggest you to lazy load the array like

@property (nonatomic, strong) NSMutableArray *latitudeArray;
/**
 *  lazy load _latitudeArray
 *
 *  @return NSMutableArray
 */
- (NSMutableArray *)latitudeArray
{
    if (_latitudeArray == nil) {
        _latitudeArray = [[NSMutableArray alloc] init];
    }
    return _latitudeArray;
}

1 Comment

I logged it before attempting to set it for the current user and it is not null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.