Hello! In the previous article, we have finished writing the base layer to handle the realm database. And today we will proceed to write services inherited from this class to use realm database flexibly.
Our RealmGithubService class will have the following:
- Store results from the returned server, specifically the GithubSearchResponse model
- Because for each keyword there will be 1 different response, and each of these keywords depends on the number of pages submitted and 1 different result. So we need to save this response with the primary key combining keyword and page
- Get saved responses based on searched keywords and corresponding pages
- Delete or update response
- Delete the entire response
From the above analysis, we proceed to create a protocol as follows:
I’ve put my name close to every idea I’ve analyzed above. However, in order for the GithubSearchResponse model to be realm-positive, we need to modify it. Let’s see what this class has:
The first is the GithubSearchItem class:
Line 49 I @objcMembers to specify that this entire class is accessible from objective-C. That’s why I did it because realm is coded from C++, so it needs to allow objective-C access. Typically, C++ cannot directly access swift, but through Objective-C. Just as java wants to access C++ then need JNI so 😝 Later you realize one thing is that you can also access React native from swift via Objective-C.
I want the GithubSearchItem class to be both usable to receive results from the API and to be able to use it as realm. Greed is real, but we will do it by editing how it can work simultaneously both with realm and codable.
- dynamic var: The columns in the realm table start with this keyword. Each property is 1 column. Here I created 4 columns to save these properties to the realm table, including id, name, htmlURL, itemDescription.
- override class func primaryKey: The function returns the primary key for the table. A table may or may not have a function that returns an optional type. However, I want this table to have a primary key for later updates. In SQL, the primary key exists to identify each line in the table, uniquely, and differently between lines. Realm works similarly. Here we see the ID has such properties so we choose to be the primary key.
- required init(from decoder: Decoder) throws: The API’s response function into a codable object. This function has the purpose that whenever the decode is finished json -> object, I create realm for this object. It is in this line:
So you can use both codable and Object of realm. Good 👌🥳
- extension for codable corresponds to each attribute and key response respectively.
Next we will look at the GithubSearchResponse class, which contains the GithubSearchItem class on:
- Similarly, this class also contains properties that can manipulate realms, so we define it as @objcMembers
- Here, because it contains the GithubSearchItem list. RealmSwift.List is required because realm does not support the usual swift array.
- Here I customize a key named keyword. The reason is that this class does not yet have a primary key, and because for each GithubSearchResponse return. So I’ll immediately think it’s the primary key. The second key depends on the page submitted, so the primary key will be the merge of the keyword and page.
- Extensions and init functions similar to githubSearchItem class
So you’ve finished the GithubSearchResponse class making sure you can save it to the realm database. Our next job is to write the RealmGithubService.😎
- As I mentioned in the previous post, every class that wants to inherit from the RealmManager class must have a uid primary key to be able to update or delete. So you’ll see the following code:
- Next, the RealmGithubService class inherits the functions of RealmGithubServiceProtocol, which we will define according to the logic of the app.
- saveRepositoryResponse: This function saves the response to the database. Before saving you need to assign a keyword to make the primary key for it:
repoList.keyword = keyword
- getRepositoryResponse: This function will query the entire model in the realm database, then find which model has keyword with keyword passed in, get the first guy and return.
- deleteRepositoryResponse: The function deletes respone by keyword. I took the model with the key is the keyword finished calling the delete function. Very simple!
- deleteAllRepositoryResponses: Delete the entire GithubSearchResponse model in the database
- Here I create the singleton class. It ensures that at one time only one class can access RealmGithubService.
Everything is almost done, now make a cup of coffee, go to the balcony to see the street and continue later.
Now let’s proceed to edit this model view. Consider the requestRepositories function already.
- Every time we request an API, we need to check if the result with that keyword already exists in the database with the following line of code:
Here you will use the Function RealmGithubService.shared.getRepositoryResponse(with: keyword) to get the response corresponding to the keyword including language + page. If it already exists, proceed to normal logic as after the API has returned the result. Otherwise, we will proceed with the request API as usual. I used the print(“from local”) function to mark data from databse, print(“from network”) to mark data from the server.
When requesting you will see the log in Xcode as follows:
Amazingly, try turning off the internet to check that it has taken data from the realm. That’s all I’m kidding about! 😄
Okey, finally, everything’s done. Now you can create an application that allows offline use such as facebook, no internet connection can still manipulate old data, to increase user experience, make your application more professional. Our application is still simple, not much complex logic. Surely I think you also understand somewhat the offline application and hip? And you see the utilities that realm brings. Hello and see you again!😘😍