如何将对象从一个Realm对象正确复制到另一个对象 [英] How can I properly copy objects from one Realm object to another object
问题描述
基于以下代码,我希望能够根据现有代码创建一个新的ItemList
.换句话说,我有一个名为First List
的ItemList
,我想创建一个新的ItemList
,将其命名为Second List
,并用First List
中的Item
填充它.
Based on the following code I would like to be able to create a new ItemList
from an existing one. In other words I have an ItemList
called First List
and I want to create a new ItemList
, call it Second List
and fill it with the Item
s from First List
.
我现在拥有的方式是,它按预期方式创建了Second List
,在Second List
中显示了First List
中的Item
,但是当我只想删除First List
中的Item
s,它将从两个列表中删除Item
.我想我不是真正地复制项目.
The way I have it right now is that it creates the Second List
as expected, the Item
s from the First List
show in Second List
but what doesn't work is when I want to delete only the Item
s from First List
, it deletes Item
s from both lists. I guess I'm not truly copying the items.
问题是,如何将Item
从First List
复制到Second List
?
So the question is, how can I copy Item
s from First List
to Second List
?
class ItemList: Object {
dynamic var listName = ""
dynamic var createdAt = NSDate()
let items = List<Item>()
}
class Item:Object{
dynamic var productName:String = ""
dynamic var createdAt = NSDate()
}
从First List
创建Second List
的代码
这很好用,它创建了Second List
并添加了First List
中的项目,但我不认为我在制作副本只是在Second List
中显示它们.
Code to create Second List
from First List
This Works fine, it creates Second List
and adds the items from First List
but I don't think I'm making copies just showing them in Second List
.
let newList = ItemList()
newList.listName = "Second List"
if let selectedList = realm.objects(ItemList.self).filter("listName = %@", "First List").first{
let itemsFromFirstList = selectedList.items
newList.items.append(objectsIn:itemsFromFirstList)
}
try! realm.write {
realm.add(newList)
}
该代码应该仅删除First List
中的项目
这实际上同时删除了First List
和Second List
This code is supposed to delete only the items from First List
This actually deletes items from both First List
and Second List
let listToDelete = realm.objects(ItemList.self).filter("listName = %@", "First List").first
try! realm.write {
for item in (listToDelete?.items)! {
realm.delete(realm.objects(Item.self).filter("productName = %@", item.productName).first!)
}
}
推荐答案
您要使用的是:
for record in postsDB.objects(PostModel.self) {
if !combinedDB.objects(PostModel.self).filter("postId == \(record.parentId)").isEmpty {
combinedDB.create(PostModel.self, value: record, update: false)
}
}
create方法是从Object继承的.它告诉目标创建一个新对象.如果希望它查看是否有记录,请使用true;如果有,请对其进行更新. PostModel是对象类型,记录是您想要复制的内容.
The create method is inherited from Object. It tells the target to create a new object. Use true if you want it to look to see if there is already a record there, and update it if there is. PostModel is the Object type, record is what you want copied.
我添加了if语句以提供更多上下文.您没有显示您的类定义,所以我在猜测.这是一个有效的例子.我要求从DatabaseA获得一组记录,然后将其复制到DatabaseB(从postsDB到CombinedDB).
I added the if statement to provide more context. You didn't show your class definitions, so I was guessing. This is a working example. I ask for a set of records from DatabaseA and copy it to DatabaseB (postsDB to combinedDB).
因此,如果要插入的对象的类型是列表,则建议您定义对象的子类,并至少将所需的列表作为属性.
So if the type of the object you're trying to insert is a List, I'd recommend you define a subclass of Object, and have at least the list you need as a property.
class TagList: Object {
dynamic var tag = ""
var list = List<PostModel>()
override class func primaryKey() -> String? {
return "tag"
}
}
完整的工作示例,其中包括:创建新对象,将所有对象复制到第二个列表,在复制后从第二个列表中删除,添加到第一个列表(不会删除任何内容.
Full working example illustrating: creating new objects, copying all objects to a second list, deleting from second list after copying, adding to first list (which didn't get anything deleted from it.
import Foundation
import RealmSwift
class Letter: Object {
dynamic var letter = "a"
}
class Letters: Object {
var letterList = List<Letter>()
}
class ListExample {
let listRealmStore = try! Realm() // swiftlint:disable:this force_try
func testThis() {
print(Realm.Configuration.defaultConfiguration.fileURL!)
listRealmStore.beginWrite()
addSingleItems() // add 3 objects to the DB
let firstList = Letters()
let allObjects = listRealmStore.objects(Letter.self)
for item in allObjects {
firstList.letterList.append(item)
}
let secondList = Letters()
let itemsToCopy = firstList.letterList
for item in itemsToCopy {
let obj = listRealmStore.create(Letter.self)
obj.letter = item.letter
secondList.letterList.append(obj)
}
let third = Letter()
third.letter = "Z"
listRealmStore.add(third)
firstList.letterList.append(third)
secondList.letterList.removeLast()
do {
try listRealmStore.commitWrite()
} catch let error {
print("couldn't commit db writes: \(error.localizedDescription)")
}
print("list one:\n\(firstList)")
print("list two:\n\(secondList)")
}
func addSingleItems() {
for letter in ["a", "b", "c"] {
let objectToInsert = Letter()
objectToInsert.letter = letter
listRealmStore.add(objectToInsert)
}
}
}
Results in:
list one:
Letters {
letterList = List<Letter> (
[0] Letter {
letter = a;
},
[1] Letter {
letter = b;
},
[2] Letter {
letter = c;
},
[3] Letter {
letter = Z;
}
);
}
list two:
Letters {
letterList = List<Letter> (
[0] Letter {
letter = a;
},
[1] Letter {
letter = b;
}
);
}
这篇关于如何将对象从一个Realm对象正确复制到另一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!