【问题标题】:Realm throws error when trying to add and save object尝试添加和保存对象时领域抛出错误
【发布时间】:2019-04-26 11:06:03
【问题描述】:

我正在使用 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 版本之前不会出现此问题。

谢谢。

【问题讨论】:

  • @BhaveshNayi 您好,我检查了 github 问题,但没有得到解决。
  • 我认为你的标志对象“measuredLength”是空的,所以这就是应用程序崩溃的原因。
  • 我明白你的意思,让我改一下代码再试一次。
  • 好的,如果有任何问题,请告诉我。

标签: ios swift realm


【解决方案1】:

您需要像这样在写事务中操作领域:

do {
    try self.realm.write
        {
        self.realm.add(entity, update: update)
         }
    }
catch {}

【讨论】:

  • 您好,抱歉,self.base 是什么,我需要在哪里添加该代码?
  • 在实体中添加你的对象@ZhengGeChe
  • 但我的代码甚至在 patient.patientImages.append(newPatientImage) 行崩溃
【解决方案2】:

这是因为每个 Flap 类(如 ohPlastyFlap、curvelinearFlap ...)都覆盖了 Flap 类并且 Realm 无法识别 Flap 类型。

我通过正确设置 Flap 类型来修复它。

【讨论】:

    【解决方案3】:

    “患者”对象缺少相应的初始化,仍然为 nil。

    之前

    patient.patientImages.append(newPatientImage)
    

    尝试为“患者”创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多