【发布时间】:2021-08-04 21:15:08
【问题描述】:
我与 Realm Schema 上的关系表有一个基本的多对多关系。 所以我有Recipe、Ingredient和RecipeIngredient模型。
当我尝试创建一个食谱时,我必须迭代一个成分数组来创建食谱成分。创建 recipeIngredient 后,我想编辑我的食谱和我的成分以添加与新 RecipeIngredient 的关系。但是成分的版本抛出异常,我不知道为什么......
这里有一些代码
配方模型
class RecipeRealm: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name: String = ""
@Persisted var lastEditedTime: Date = Date()
@Persisted var recipeIngredients: List<RecipeIngredient>
convenience init(name: String){
self.init()
self.name = name
}
}
成分模型
class IngredientRealm: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name: String = ""
@Persisted var unit: Unit = Unit.none
@Persisted var lastEditedTime: Date = Date()
@Persisted var recipeIngredients: List<RecipeIngredient>
convenience init(name: String){
self.init()
self.name = name
}
}
配方成分模型
class RecipeIngredient: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var qty: Int = 0
@Persisted var lasEditedTime: Date = Date()
@Persisted(originProperty: "recipeIngredients") var recipe: LinkingObjects<RecipeRealm>
@Persisted(originProperty: "recipeIngredients") var ingredient: LinkingObjects<IngredientRealm>
}
配方表单模型
struct QuantifiedIngredient {
var ingredient: IngredientRealm
var qty: Int16
}
class RecipeForm: ObservableObject {
@Published var name = ""
@Published var ingredients: [QuantifiedIngredient] = []
var recipeID: String?
var updating: Bool {
recipeID != nil
}
init(){}
init(_ recipe: Recipe){
name = recipe.name ?? ""
ingredients = []
recipeID = recipe.id
}
}
创建配方逻辑
private func createRecipe() {
withAnimation {
do {
let realm = try Realm()
// Create Recipe on RealmDB
let realmRecipe = RecipeRealm(name: form.name)
try realm.write{
realm.add(realmRecipe)
for ingredient in form.ingredients{
let recipeIngredient = RecipeIngredient()
recipeIngredient.qty = Int(ingredient.qty)
realm.add(recipeIngredient)
realmRecipe.recipeIngredients.append(recipeIngredient)
ingredient.ingredient.recipeIngredients.append(recipeIngredient) // <--- this line crash
}
}
} catch {
print(error.localizedDescription)
}
presentationMode.wrappedValue.dismiss()
}
}
createRecipe 方法上的这一行抛出异常:ingredient.ingredient.recipeIngredients.append(recipeIngredient)。
这里的错误信息:Terminating app due to uncaught exception 'RLMException', reason: 'Cannot modify managed RLMArray outside of a write transaction.'
但是我有写块...
有什么想法吗?
【问题讨论】:
-
我会知道是哪一行引发了错误。请逐行检查您的代码以确定这一点并使用信息更新问题。
-
谢谢。我在代码中提到过,这一行在 create recipe 方法中抛出了错误
ingredient.ingredient.recipeIngredients.append(recipeIngredient)。我会更新我的问题 -
如果我在看这个,
for ingredient in form.ingredients正在迭代form中的成分,class RecipeForm: ObservableObject不是 Realm 对象。所以你想将非领域对象添加到领域?或者也许还有更多。 -
你是对的
form,不是一个领域对象,但它包含一个QuantifiedIngredient数组,其中包含一个ingredient这是一个领域对象,这就是我这样做的原因ingredient.ingredient(领域对象).recipeIngredients.append() -
所以这个
ingredient.ingredient.recipeIngredients.解析为QuantifiedIngredient.ingredient.recipieIngredients,对吧?那么QuantifiedIngredient是什么,它是否由同一个领域管理,和/或它是一个领域对象?