Basic CRUD
Before working with the Skygear records, we suggest you reading the basics.
Creating records
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
todo
record and assigned some attributes to it. you can use the[]
subscript operator as shown above, or thesetObject: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
todo
record 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 asSKYRecordID
,recordName
,creationDate
andmodificationDate
. ASKYRecord
will be returned and you can make use of the block to add additional logic which will run after the save completes.
Creating multiple records
You can also save multiple SKYRecord
s at once with
saveRecords
or
saveRecordsAtomically
which make sure the save
operation either succeeds or fails as a whole:
Updating records
Now let's return to our to-do item example. This is how you save a SKYRecord
:
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
. The recordID
property on your saved todo SKYRecord
is a unique recordID
which identifies the record in a database.
With the 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.
Querying records
With the 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 objectForKey:
method:
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 records
Deleting a record
Deleting a record requires its recordID
too:
If you are to delete records in batch, you could also use the
SKYDatabase-deleteRecordsWithIDs:completionHandler:perRecordErrorHandler:
method.
Deleting multiple records
You can also delete multiple records at once: