Realm似乎一直在iOS设备上重置数据库

时间:2016-12-15 14:14:28

标签: ios swift realm

我似乎无法弄清楚为何或何时Realm.io决定删除我存储在文档目录中的文件。通过在一段时间后通过写事件进行监视,文件不再可用。

我无法阅读有关对文件进行任何手动写入的文档中的任何内容,但是localStorage是持久的非常重要。

在模拟器上,我可以直接在桌面上使用URL而不是在模拟器中修复此问题。但这当然不适用于iOS。

手动writeCopy(to:_)似乎没有帮助。

配置:

let cfg = Realm.Configuration(
            fileURL: Database.baseURL(),
            inMemoryIdentifier: nil,
            syncConfiguration: nil,
            encryptionKey: _password,
            readOnly: false,
            schemaVersion: 1,
            migrationBlock: { migration, oldVersion in
            },
            deleteRealmIfMigrationNeeded: false,
            objectTypes: nil
        )

更新#1

编写代码的示例:

internal func dbRow() -> MessageRow {
        let row = MessageRow()

        row.id = id
        row.state = Int(status.rawValue)
        row.remoteID = remoteID
        row.receiverID = receiver.id
        row.senderID = author.id
        row.filePath = filePath
        row.message = text
        row.timestamp = timestamp.timeIntervalSince1970
        row.burnTime = burnTime
        return row
    }

    internal func update() {
        try! Database.default.realm.write {
            Database.default.realm.add(self.dbRow(), update: true)
        }
    }

    internal func save(outGoing: Bool = false) {
        if outGoing == true {
            guard let row = receiver.row else { return }

            try! Database.default.realm.write {
                row.messages.append(self.dbRow())
            }
            return
        }
        guard let row = author.row else { return }

        try! Database.default.realm.write {
            row.messages.append(self.dbRow())
        }
    }

MessageRow.swift:

class MessageRow: Object {
    dynamic var id:            Int = 0
    dynamic var message:    String = ""
    dynamic var state:         Int = 0
    dynamic var senderID:      Int = 0
    dynamic var receiverID:    Int = 0
    dynamic var remoteID:      Int = 0
    dynamic var burnTime:      Int = 0
    dynamic var timestamp:  Double = 0
    dynamic var filePath:  String? = nil

    override static func primaryKey() -> String {
        return "id"
    }
}

receiver.row包含之前从Realm获取的UserRow实例,其定义如下:

internal class UserRow: Object {
    dynamic var id:       Int = 0

    dynamic var firstName: String = ""
    dynamic var lastName:  String = ""
    dynamic var infix:     String = ""
    dynamic var clinic:    String = ""
    dynamic var avatarURL: String = ""
    dynamic var clinicalFocus: String = ""
    dynamic var position:  String = ""
    dynamic var lastOnline:  Date = Date()
    dynamic var publicKey:   Data = Data()
    dynamic var isPending:   Bool = false

    var messages: List<MessageRow> = List<MessageRow>()

    override static func primaryKey() -> String {
        return "id"
    }
}


Optional(UserRow {
    id = 2;
    firstName = Helpdesk;
    lastName = MDLinking;
    infix = ;
    clinic = ;
    avatarURL = https://s3-eu-west-1.amazonaws.com/mdlinking-dev/2016/10/21/images/f1fb4e96-7767-4e99-b3e0-7a7c8b9dca04.png;
    clinicalFocus = ;
    position = ;
    lastOnline = 2016-12-15 16:35:01 +0000;
    publicKey = <93ce6666 7ca47759 184915ec a5efb4df 1b814309 06421b0b — 32 total bytes>;
    isPending = 0;
    messages = RLMArray <0x1702f1f00> (
        [0] MessageRow {
            id = 1;
            message = cyka blyat idi nahuiy.;
            state = -1;
            senderID = 2;
            receiverID = 1;
            remoteID = 35;
            burnTime = 0;
            timestamp = 1481819648;
            filePath = ;
        },
        [1] MessageRow {
            id = 2;
            message = test message;
            state = -1;
            senderID = 2;
            receiverID = 1;
            remoteID = 36;
            burnTime = 0;
            timestamp = 1481819776;
            filePath = ;
        }
    );
}) Optional(RealmSwift.Realm)

0 个答案:

没有答案