我正在使用Realm库开发iOS应用,效果很好。
但是,当我尝试保存一个模型时,会引发如下错误。
由于未捕获的异常“ RLMException”而终止应用程序,原因: “目标表行索引超出范围
这里是代码。
....
do {
try realm?.write {
let newPatientImage = PatientImage()
newPatientImage.path = imageName
if let flap = flap {
newPatientImage.setStronglyTypedFlap(flap)
}
newPatientImage.hasPhoto = hasPhoto
newPatientImage.flap?.flapLocationRaw = flapLocation.rawValue
newPatientImage.id = newPatientImage.incrementalID()
patient.patientImages.append(newPatientImage)
realm?.add(newPatientImage)
realm?.add(patient, update: true)
}
} catch {
}
...
这里是newPatientImage对象。
PatientImage {
id = 22;
hasPhoto = 0;
isPostOp = 0;
path = 4426699712;
date = 2019-04-26 11:32:15 +0000;
flap = RhomboidAFlap {
id = 1;
name = Rhomboid A;
adjustables = List<Adjustable> <0x280b5c120> (
[0] Adjustable {
value = 60;
defaultValue = 60;
name = A;
isDegrees = 1;
},
[1] Adjustable {
value = 60;
defaultValue = 60;
name = B;
isDegrees = 1;
},
[2] Adjustable {
value = 200;
defaultValue = 200;
name = C;
isDegrees = 0;
},
[3] Adjustable {
value = 200;
defaultValue = 200;
name = E;
isDegrees = 0;
}
);
a = 0.6491648060943035;
b = 0;
c = 0;
d = 0.6491648060943035;
tx = -195.6666717529297;
ty = -144.5;
xOffset = -44.99840983089462;
yOffset = 30.7939660691552;
width = 650;
height = 1000;
centerX = 429.5;
centerY = 499.9999999999999;
calibrationSettings = CalibrationSettings {
measurementUnitRaw = px;
conversionRatio = 1;
measuredLength = 0;
startX = 0;
startY = 0;
endX = 0;
endY = 0;
};
flapLocationRaw = ;
};
ohPlastyFlap = (null);
curvelinearFlap = (null);
rhomboidAFlap = RhomboidAFlap {
id = 1;
name = Rhomboid A;
adjustables = List<Adjustable> <0x280b5c120> (
[0] Adjustable {
value = 60;
defaultValue = 60;
name = A;
isDegrees = 1;
},
[1] Adjustable {
value = 60;
defaultValue = 60;
name = B;
isDegrees = 1;
},
[2] Adjustable {
value = 200;
defaultValue = 200;
name = C;
isDegrees = 0;
},
[3] Adjustable {
value = 200;
defaultValue = 200;
name = E;
isDegrees = 0;
}
);
a = 0.6491648060943035;
b = 0;
c = 0;
d = 0.6491648060943035;
tx = -195.6666717529297;
ty = -144.5;
xOffset = -44.99840983089462;
yOffset = 30.7939660691552;
width = 650;
height = 1000;
centerX = 429.5;
centerY = 499.9999999999999;
calibrationSettings = CalibrationSettings {
measurementUnitRaw = px;
conversionRatio = 1;
measuredLength = 0;
startX = 0;
startY = 0;
endX = 0;
endY = 0;
};
flapLocationRaw = ;
};
rhomboidBFlap = (null);
noteFlap = (null);
vyPlastyFlap = (null);
bilobedFlap = (null);
circularZFlap = (null);
atPlastyFlap = (null);
ouPlastyAFlap = (null);
ouPlastyBFlap = (null);
simpleEllipseFlap = (null);
}
这是耐心的对象。
Patient {
id = 1;
firstName = Gg;
lastName = ;
email = ;
phone = ;
address1 = ;
address2 = ;
city = ;
state = ;
dateOfBirth = 2019-04-03 21:24:28 +0000;
medicalRedcordNumber = ;
imageUrl = avatar_1;
patientImages = List<PatientImage> <0x2834a5200> (
[0] PatientImage {
id = 1;
hasPhoto = 1;
isPostOp = 0;
path = 4489901936;
date = 2019-04-08 16:21:54 +0000;
flap = (null);
ohPlastyFlap = (null);
curvelinearFlap = (null);
rhomboidAFlap = (null);
rhomboidBFlap = (null);
noteFlap = (null);
vyPlastyFlap = (null);
bilobedFlap = (null);
circularZFlap = (null);
atPlastyFlap = (null);
ouPlastyAFlap = (null);
ouPlastyBFlap = (null);
simpleEllipseFlap = (null);
}
}
任何人都可以帮助我解决此问题吗? 在更新iOS版本和Realm版本之前不会发生此问题。
谢谢。
答案 0 :(得分:1)
这是因为每个Flap类(如ohPlastyFlap,curvelinearFlap ...)都会覆盖Flap类,而Realm无法识别Flap类型。
我通过正确设置襟翼类型来修复它。
答案 1 :(得分:0)
您需要像这样在写事务中操作领域:
open System
open LiteDB
open LiteDB.FSharp
[<StructuredFormatDisplay("Article #{Id}: {Name}")>]
type Article( Id: int, Name: string) as self =
let fullName = (string Id) + " " + Name
member val Id = Id //Read Only
member val Name = Name with get, set //Mutable
//[<BsonIgnore>]
member this.FullName with get() = fullName
//[<BsonIgnore>]
member this.FullName2 = (string Id) + " " + Name
[<EntryPoint>]
let main argv =
let mapper = FSharpBsonMapper()
use db = new LiteDatabase("simple.db", mapper)
let articles = db.GetCollection<Article>("articles")
let id = BsonValue(2)
// Insert
let art2 = new Article(2, "First Name")
printfn "%A" art2
printfn "fn: %s" art2.FullName
articles.Insert(art2) |> ignore
printfn "==="
// Find
let result = articles.FindById(id)
printfn "%A" result
printfn "fn: %s" result.FullName
printfn "==="
// Modify
result.Name <- "New Name"
printfn "%A" result
printfn "fn: %s" result.FullName //<- No Modified!
printfn "fn2: %s" result.FullName2 //<- No Modified!
printfn "==="
// Update Modified Changes
articles.Update(result) |> ignore
let result = articles.FindById(id)
// printfn "%%O %O" result //.ToString()
printfn "%A" result
printfn "fn: %s" result.FullName
0 // return an integer exit code
答案 2 :(得分:0)
“患者”对象缺少相应的初始化,仍然为零。
之前
patient.patientImages.append(newPatientImage)
尝试为“患者”创建。