问题是如何使对象在同一个线程中观察和commitWrite。
realm.commitWrite(withoutNotifying: [token])来实现commitWrite。下面的代码是commitWrite,没有通知。
我想用
BackgroundWorker在同一个线程中运行这段代码
let realm = try! Realm()
realm.beginWrite()
realm.add(object, update: true)
try! realm.commitWrite(withoutNotifying: [notificationToken])这是王国的物体观察。它将在子线程中运行(BackgroundWorker来自上面的链接)
var worker: BackgroundWorker?
private func registerLocalDatabaseStep1() {
worker = BackgroundWorker()
worker?.start { [weak self] in
guard let `self` = self else { return }
`self`.registerLocalDatabase()
}
}
private func registerLocalDatabase() {
let objects = Cream<T>().realm.objects(T.self)
notificationToken = objects.observe({ [weak self](changes) in
guard let `self` = self else { return }
switch changes {
case .initial(_):
break
case .update(let collection, _, let insertions, let modifications):
//do something
case .error(_):
break
}
})
}我从上面的链接中发布了
BackgroundWorker,以使它更容易阅读。
class BackgroundWorker: NSObject {
private var thread: Thread!
private var block: (()->Void)!
internal func runBlock() { block() }
internal func start(_ block: @escaping () -> Void) {
self.block = block
let threadName = String(describing: self)
.components(separatedBy: .punctuationCharacters)[1]
thread = Thread { [weak self] in
while (self != nil && !self!.thread.isCancelled) {
RunLoop.current.run(
mode: RunLoopMode.defaultRunLoopMode,
before: Date.distantPast)
}
Thread.exit()
}
thread.name = "\(threadName)-\(UUID().uuidString)"
thread.start()
perform(#selector(runBlock),
on: thread,
with: nil,
waitUntilDone: false,
modes: [RunLoopMode.defaultRunLoopMode.rawValue])
}
public func stop() {
thread.cancel()
}
}根据领域API

问题是如何使对象在同一个线程中观察和commitWrite。
我想使用realm.commitWrite(withoutNotifying方法在同一个线程中运行registerLocalDatabase()。
发布于 2018-02-07 06:02:53
答案是,在同一个线程中运行代码很容易。
更新BackgroundWorker类
class BackgroundWorker: NSObject {
private var thread: Thread!
private var block: (()->Void)!
@objc internal func runBlock() { block() }
///create a thread
private func createThread() {
let threadName = String(describing: self)
.components(separatedBy: .punctuationCharacters)[1]
thread = Thread { [weak self] in
while (self != nil && !self!.thread.isCancelled) {
// Poll to see if thread was canceled. If using
// multiple threads `.distantPast` will cause 100% CPU
// usage. If using `.distantFuture` the thread will never exit.
RunLoop.current.run(
mode: RunLoopMode.defaultRunLoopMode,
before: .now + 30)
}
Thread.exit()
}
thread.name = "\(threadName)-\(UUID().uuidString)"
thread.start()
}
internal func start(_ block: @escaping () -> Void) {
self.block = block
//if thread is nil, create a new one. Otherwise run block in the same thread!!!!
if thread == nil {
createThread()
}
perform(#selector(runBlock),
on: thread,
with: nil,
waitUntilDone: false,
modes: [RunLoopMode.defaultRunLoopMode.rawValue])
}
public func stop() {
thread.cancel()
}
}因此,如何执行,使用相同的
workerofBackgroundWorker:
`self`.worker?.start {
let realm = try! Realm()
realm.beginWrite()
realm.add(object, update: true)
realm.commitWrite(withoutNotifying: [`self`.notificationToken])
}发布于 2018-02-07 05:34:36
我相信您可以将提交commitWrite移动到主线程,如下所示:
DispatchQueue.main.sync {
let realm = try! Realm()
realm.beginWrite()
realm.add(object, update: true)
try! realm.commitWrite(withoutNotifying: [notificationToken])
}https://stackoverflow.com/questions/48656511
复制相似问题