简体   繁体   中英

Realm throws error when trying to add and save object

I'm developing iOS app with Realm library and it works fine.
But when I tried to save one model it throws error like this.

Terminating app due to uncaught exception 'RLMException', reason: 'Target table row index out of range

Here's code.

....
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 {

}
...

Here's newPatientImage object.

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

And Here's patient object.

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

Could anyone please help me resolve this issue? This issue doesn't happen before updating iOS version and Realm version.

Thanks.

You need to manipulate realm in a write transaction like this:

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

It was because every Flap classes(like ohPlastyFlap, curvelinearFlap ...) override Flap class and Realm doesn't recognize Flap type.

I fixed it by correctly setting Flap type.

The "patient" object is missing the corresponding initialization, which is still a nil.

Before

patient.patientImages.append(newPatientImage)

try creating for "patient".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM