简体   繁体   English

领域:一次写多个

[英]Realm : multiple write at the same time

Context 语境

I have problems using the method realm.add(object, update: true) successively. 我先后使用realm.add(object, update: true)方法遇到问题。

I'm making WEB requests to my API, and I'm testing the case where internet connection is disabled. 我正在向我的API发出WEB请求,并且正在测试禁用Internet连接的情况。 When a request failed and I get the response, I add an UnsynchronizedRequest realm object to Realm . 当请求失败并获得响应时,我将UnsynchronizedRequest领域对象添加到Realm

Problem 问题

I have to test the case where I have to make multiples call to my API, so I will add multiple UnsynchronizedRequest objects to Realm . 我必须测试必须对API进行多次调用的情况,因此我要将多个UnsynchronizedRequest对象添加到Realm

I only start the next web request when I received the previous request response, so the requests are well chaining, there is no concurrent requests. 我仅在收到上一个请求响应时才启动下一个Web请求,因此请求链接良好,没有并发请求。

When I'm making only one request and that it failed, the object is well added to Realm . 当我发出一个请求并且该请求失败时,该对象就会很好地添加到Realm

But when I'm making more than one request , only the first is added to the Realm, and the others are not added. 但是,当我发出多个请求时只会将第一个添加到Realm中,而不会添加其他请求

What is strange is that when I'm using breakpoints , all objects are well added to Realm . 奇怪的是,当我使用断点时所有对象都很好地添加到了Realm中 But when I disable breakpoints, only the first UnsynchronizedRequest object is added to the Realm, the other are ignored. 但是,当我禁用断点时, 只有第一个UnsynchronizedRequest对象被添加到领域中,其他对象被忽略。

I think it is a problem with the write thread, the doc says that it blocks other thread if multiple writes are concurrent, but I don't know how to solve it. 我认为这是write线程的问题,文档说如果并发多个写操作,它将阻塞其他线程,但是我不知道如何解决。

What I execute when a web request failed : Web请求失败时执行的操作:

class func createUnsynchronizedRequest(withParameters parameters : Parameters, andRoute route : String){
    print("Adding unsynchronized request to local...")
    let failedRequest = UnsynchronizedRequest()
    failedRequest.parameters = parameters
    failedRequest.route = route
    failedRequest.date = Date()
    RealmManager.sharedInstance.addOrUpdate(object: failedRequest)
}

RealmManager.swift RealmManager.swift

func addOrUpdate(object : Object){
    try! realm.write {
        realm.add(object, update: true)
    }
}

UnsynchronizedRequest.swift UnsynchronizedRequest.swift

@objcMembers class UnsynchronizedRequest : Object {


// MARK: - Realm Properties

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

dynamic var id = ""
dynamic var route = ""
dynamic var date : Date! {
    didSet {
        self.id = "\(self.date)"
    }
}
}

I already tried to check if realm was in transaction in the addOrUpdate method before starting realm.write , this not solve the problem. 我已经尝试在启动realm.write之前在addOrUpdate方法中检查领域是否在事务中,这不能解决问题。

I also tried to catch the error with realm.write , but no error is thrown. 我还尝试使用realm.write捕获错误,但未引发任何错误。

Furthermore, if I execute for example 3 time a web request et that is failed, I'm sure that my code is executed because the print in createUnsynchronizedRequest is working. 此外,例如,如果我执行了3次失败的Web请求,则我确定我的代码已执行,因为createUnsynchronizedRequestprint正常。

I really think it is a problem with write threads because when I use breakpoint to slow the code execution, everything works well. 我真的认为这是write threads的问题,因为当我使用断点来减慢代码执行速度时,一切都很好。

Example with 3 failing web requests and USING BREAKPOINTS : 3个失败的Web请求和USING BREAKPOINTS的示例

在此处输入图片说明

As you can see, using breakpoints, the 3 objects are well added to realm : 如您所见,使用断点,将这三个对象很好地添加到了领域:

在此处输入图片说明

Example with the same 3 failing web requests and WITHOUT BREAKPOINTS : 相同的3个失败的Web请求和WITHBREAK BREAKPOINTS的示例

在此处输入图片说明

But now without breakpoints, only the first object is added to Realm : 但是现在没有断点,只有第一个对象被添加到Realm:

在此处输入图片说明

Any idea ? 任何想法 ?

As I created my UnsynchronizedRequest objects with the Date() method, the created objects had the same primary keys. 当我使用Date()方法创建UnsynchronizedRequest对象时,创建的对象具有相同的主键。

The space time between object creation is not sufficient to make a Date() object different than the previous created. 创建对象之间的时空不足以使Date()对象不同于先前创建的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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