How to use RLMArray to save an array

Note: I'm pretty new to Realm and Swift, so sorry any obvious things that I don't understand.

I have a working UITableView that I plan to populate with tasks. I want the user to be able to add and remove tasks as needed, so I can’t hard-set tasks, and I want tasks to be saved between application launches. The most obvious way to do this is to create an array that will be stored in Realm. The problem is that I do not understand how to save an array in Realm. I read the documentation on the Realm website, but found it rather confusing due to the fact that I was still fairly new to Swift (and this is in ObjC). How would you create an array and save it? I initially tried this, and when it didn’t work, I did some research and found that RLMArray is actually not an array:

let ToDoTasksArray: RLMArray = ["Task Goes Here", "Task Goes Here2"] 

Any help on this? Thanks!

Edit:

I also tried saving the array in RLMObject as an object like this:

 realm.addObject(ToDoTasksArray) 

But this also does not work, which does not surprise me. An array is not an object.

+5
source share
2 answers

first you need to customize your models so that your todo can be saved in the area.

All you have to do is have it in one of your files (preferably the todo.swift file)

 class Todo: RLMObject { dynamic var name = "" } 

Then you can create your first two icons by following these steps:

 var firstTodo = Todo() firstTodo.name = "My first thing todo!" var secondTodo = Todo() secondTodo.name = "My second thing todo!" 

Then you can save it in the kingdom

 let realm = RLMRealm.defaultRealm() realm.beginWriteTransaction() realm.addObject(firstTodo) realm.addObject(secondTodo) realm.commitWriteTransaction() 

Now you can capture all your todos that return an array of them

 let arrayOfTodos = Todo.allObjects() 

If I were to create a method to save new todos, I would do something like this

 func createNewTodo(todo: String){ let todo = Todo() todo.name = todo let realm = RLMRealm.defaultRealm() realm.beginWriteTransaction() realm.addObject(todo) realm.commitWriteTransaction() } 
+4
source

Realm saves objects obtained from RLMObject, so you need to define a class for your task, for example:

 @interface Task : RLMObject @property NSString * task; @property NSString * description; @property NSDate * dueDate; ... @end RLM_ARRAY_TYPE(Task) // define RLMArray<Task> 

Then create a task list model as:

 @interface TaskList : RLMObject @property RLMArray<Task> * tasks; @end 

Now you create a Task, add it to the TaskList and save:

 RLMRealm * realm = [RLMRealm defaultRealm]; [realm beginWriteTransaction]; Task * task = [Task new]; task.task = @"Some new task"; RLMArray <TaskList> * tasksLists = (RLMArray <TaskList> *)[TaskList allObjects]; // You can manage multiple task lists here using unique primary key for each task list. I am assuming that we have only one list. TaskList * taskList = tasksLists.firstObject; [taskList.tasks addObject: task]; [realm addOrUpdateObject: taskList]; [realm commitWriteTransaction]; 

Hope this helps.

Sorry, I forgot that you mentioned using Swift.

+8
source

Source: https://habr.com/ru/post/1213653/


All Articles