How to use RLMArray to save an Array

瘦欲@ 提交于 2019-12-01 16:48:04

问题


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.


回答1:


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()
}



回答2:


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.



来源:https://stackoverflow.com/questions/28593683/how-to-use-rlmarray-to-save-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!