简体   繁体   中英

Saving and retrieving data from parse using swift

I was successfully able to save data (integer) to parse using swift.

var gameScore = PFObject(className:"GameScore")
gameScore["score"] = highscore

I have successfully stored in data in an objectId named "EN34LdmbSB" I found this objectId by logging into parse.com and looking it up. I have later unsuccessfully tried to use same objectId to retrieve data and it did not work. I have to 2 questions: 1) How to I retrieve this data from parse. 2) If this app is downloaded, say for example by 1000 users, then 1000 unique objectIds will be created when users save their score. How do I then look up their score using their unique objectIds?

I am new to database and Parse so please pardon my naivety. I am currently using NSUserDefaults to store my game score. I am trying to get a more secure way of storing the score data, hence using parse. Tried using KeyChain but did not get very far. Thank you

You can easily retrieve the data using one of their method in iOS SDK:

PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
[query whereKey:@"playerName" equalTo: highscore];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // The find succeeded.
    NSLog(@"Successfully retrieved %d scores.", objects.count);
    // Do something with the found objects
    for (PFObject *object in objects) {
        NSLog(@"%@", object.objectId);
    }
  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];

You can look it up in the tutorial they provided and it's really helpful and saving time: https://parse.com/docs/ios_guide#queries/iOS

For you second question, I don't quite get what you want to ask. Parse built the backend for you so that you don't have to create database to manage your data. I suggested you to go check what Parse truly does or you can modify it so that I can understand better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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