Before working with the Skygear records, we suggest you reading the basics.
Creating a record
Let's imagine we are writing a To-Do app with Skygear. When user creates an to-do item, we want to save that item on server. We probably will save that to-do item like this:
There are couples of things we have done here:
- First we created a
todorecord and assigned some attributes to it. you can use the
subscript operator as shown above, or the
setObject:forKey:method. Your app automatically creates this Class when you first use it.
- We fetched the container of our app, and took a reference to the private database of the current user. So when you save a record, you're saving the record to the private database of the current user.
- We actually saved the
todorecord and registered a block to be executed after the action is done. When you have successfully saved a record, there are several fields automatically filled in for you, such as
SKYRecordwill be returned and you can make use of the block to add additional logic which will run after the save completes.
Creating multiple records
Now let's return to our to-do item example. This is how you save a
After you have successfully saved the
SKYRecord, the server will return an updated
SKYRecord. Your console should look like this:
2015-09-22 16:16:37.893 todoapp[89631:1349388] saved todo with recordID = <SKYRecordID: 0x7ff93ac37940; recordType = todo, recordName = 369067DC-BDBC-49D5-A6A2-D83061D83BFC>
As you can see, the returned
SKYRecord now has a
recordID property on your saved todo
SKYRecord is a unique
recordID which identifies the record in a database.
recordID you can modify the record later on. Say if
you have to mark this todo as done:
Note that the data in the returned record in the completion block may be
different from the originally saved record. This is because additional
fields maybe applied on the server side when the record is saved (e.g. the updated
modificationDate). You may want to inspect the returned record for any changes applied on the server side.
recordID we could also fetch the record from a database:
In Objective-C, to get the values out of the
SKYRecord, you can use the
 subscript operator as shown above, or the
NSString *title = [record objectForKey: @"title"]; NSNumber *order = [record objectForKey: @"order"]; NSNumber *done = [record objectForKey: @"done"];
You can construct a
SKYQuery object by providing a
recordType. You can configure the
SKYQuery by mutating its state. Read the More About Queries section to learn more.
Deleting a record
Deleting a record requires its
If you are to delete records in batch, you could also use the
Deleting multiple records
You can also delete multiple records at once: