Local database storage
Defining the database object
SDK provided by GSD gives the developer the possibility to easily store data in local database. The data can be easily readed, modified and deleted.
To store object inside the database one hase to create his own class which will inherit after GSDDatabaseOject
Example:
1 2 3 4 | open class ExampleObject: GSDDatabaseObject { dynamic var title: String! dynamic var description: String! } |
To be able to store a property inside the database, it has to be declared with dynamic
keyword.
Storing of the data
After creation of object one can easily store it inside the database with store
function.
1 2 3 4 | let tempObject = ExampleObject(); tempObject.title = "Title" tempObject.description = "Description" tempObject.store() |
store
function automatically opens the write transcation which will store the element in the database.
Reading of the data
To read the data from database one has to use GSDDatabaseManagerClass. First one has to get objects from the database. It will return the list of pointers to objects. Then the list of pointers will have to be casted to array type.
Example:
1 2 | var results = GSDDatabaseManager().getObjects(type: ExampleObject.self) var exampleObjects = results.toArray(ofType: ExampleObject.self) |
Updating of the data
When someone will get the objects from database he can update it with usage of store function. SDK mechanizm will automatically create a primary key for every object stored in the database. So after storing the object, we are not creating another object with the same data, but we are updating already exising instance.
1 2 3 4 | for exampleObject in exampleObjects { exampleObject.title = "Modified title" exampleObject.store } |
Deleting
To delete object from database, one has to simply use deleteObject()
function on fetched object.
1 2 3 | for exampleObject in exampleObjects { exampleObject.deleteObject() } |
Deletinon of object will only delete an instance in the database. The instance which is used in local memory will be still accessible. The function will return the array of objects