Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RLMArray to save an Array

Tags:

ios

swift

realm

Note: I am fairly new to Realm and Swift, so excuse any obvious things I don't understand.

I have a working UITableView which I plan to populate with tasks. I want the user to be able to add and delete tasks as needed, so I can't hardcode the tasks, and I want the tasks to be saved between app launches. The most obvious way to do this would be to create an array that gets saved in Realm. The problem is, I don't understand how to save an array in Realm. I have read the documentation on the Realm site but have found it rather confusing due to myself still being fairly new to Swift (And that it's in ObjC). How would you create an array and save it? I originally tried this, and when it didn't work I did some research and found that RLMArray isn't actually an array:

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

Any help on this? Thanks!

Edit:

I have also tried saving an array into an RLMObject as an object as so:

realm.addObject(ToDoTasksArray)

But that also doesn't work, which doesn't surprise me. An array is not an object.

like image 604
CodeIt Avatar asked Feb 18 '15 20:02

CodeIt


2 Answers

Realm saves objects derived from RLMObject, so you need to define class for your task like:

@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 Task, add it to 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 overlooked that you mentioned you are using Swift.

like image 172
gagarwal Avatar answered Sep 28 '22 18:09

gagarwal


you first need to setup your models so that your todo's can be saved to the realm.

All you need to do is have this in one of your files (Preferably a todo.swift file)

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

Then you can create your first two todos by doing this:

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

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

Then you can save it to the realm

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

Now you can grab all of your todos which returns an array of them

let arrayOfTodos = Todo.allObjects()

If I were to create a method to save new todos I'd 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()
}
like image 43
yoshyosh Avatar answered Sep 28 '22 18:09

yoshyosh