macos - Realm 在编辑操作中可以与 Cocoa Binding 一起使用吗?

标签 macos cocoa realm cocoa-bindings

我正在使用 NSOutlineView 并绑定(bind)到 Realm 对象数组。它适用于使用数据绑定(bind)显示属性。

当我尝试编辑该项目时,它会抛出以下错误:

2015-12-28 12:21:13.433 My_Mac_App[87665:3922875] Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.
2015-12-28 12:21:13.436 My_Mac_App[87665:3922875] (
    0   CoreFoundation                      0x00007fff830d4e32 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff9303e4fa objc_exception_throw + 48
    2   Realm                               0x00000001002a9127 _ZL27RLMVerifyInWriteTransactionP13RLMObjectBase + 103
    3   Realm                               0x000000010029f6bd _ZL11RLMSetValueP13RLMObjectBasemP8NSString + 29
    4   Realm                               0x00000001002a9c8c _ZZZL13RLMMakeSetterIU8__strongP8NSStringS2_EPFvvEP11RLMPropertyEUb1_ENKUlvE_clEv + 44
    5   Realm                               0x00000001002a6d20 _ZL13RLMWrapSetterIZZL13RLMMakeSetterIU8__strongP8NSStringS3_EPFvvEP11RLMPropertyEUb1_EUlvE_EvP13RLMObjectBaseS2_OT_ + 176
    6   Realm                               0x00000001002a6c30 ___ZL13RLMMakeSetterIU8__strongP8NSStringS2_EPFvvEP11RLMProperty_block_invoke_2 + 96
    7   Foundation                          0x00007fff8abd5674 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 424
    8   Foundation                          0x00007fff8abf8276 -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 287
    9   AppKit                              0x00007fff956b4640 -[NSView setValue:forKeyPath:] + 398
    10  AppKit                              0x00007fff956f0e4a -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 411
    11  AppKit                              0x00007fff956f0c58 -[NSBinder setValue:forBinding:error:] + 248
    12  AppKit                              0x00007fff95b9838b -[NSValueBinder _applyObjectValue:forBinding:canRecoverFromErrors:handleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:] + 220
    13  AppKit                              0x00007fff95b986d6 -[NSValueBinder applyDisplayedValueHandleErrors:typeOfAlert:canRecoverFromErrors:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 566
    14  AppKit                              0x00007fff95b98861 -[NSValueBinder _applyDisplayedValueIfHasUncommittedChangesWithHandleErrors:typeOfAlert:discardEditingCallback:otherCallback:callbackContextInfo:didRunAlert:error:] + 117
    15  AppKit                              0x00007fff9576a5e2 -[NSValueBinder validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 484
    16  AppKit                              0x00007fff9576a3d7 -[_NSBindingAdaptor _validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:bindingAdaptor:] + 162
    17  AppKit                              0x00007fff9576a316 -[_NSBindingAdaptor validateAndCommitValueInEditor:editingIsEnding:errorUserInterfaceHandled:] + 246
    18  AppKit                              0x00007fff955ce6a4 -[NSTextField textShouldEndEditing:] + 374
    19  AppKit                              0x00007fff955ba950 -[NSTextView(NSSharing) resignFirstResponder] + 493
    20  AppKit                              0x00007fff954af01f -[NSWindow _realMakeFirstResponder:] + 228
    21  AppKit                              0x00007fff954aeee7 -[NSWindow makeFirstResponder:] + 123
    22  AppKit                              0x00007fff95bb5109 -[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 5518
    23  AppKit                              0x00007fff95bb6411 -[NSWindow _reallySendEvent:isDelayedEvent:] + 212
    24  AppKit                              0x00007fff955fbb8d -[NSWindow sendEvent:] + 517
    25  AppKit                              0x00007fff9557bb27 -[NSApplication sendEvent:] + 2540
    26  AppKit                              0x00007fff953e2d9a -[NSApplication run] + 796
    27  AppKit                              0x00007fff953abfbe NSApplicationMain + 1176
    28  My_Mac_App                          0x000000010004fc37 main + 87
    29  libdyld.dylib                       0x00007fff903715ad start + 1
)

是否可以使用数据绑定(bind),并且能够编辑?

还有其他使用方法吗?

最佳答案

Realm 不提供对 Cocoa Bindings 的专门支持。但由于它们基于受支持的 KVC/KVO,因此至少对于独立对象应该没问题。如果您想使用附加/持久对象,您需要确保自己这些修改是在写入事务中进行的。

我不确定最好的位置在哪里,但堆栈跟踪应该给出一些想法。您可以尝试在将 -[NSTextField textShouldEndEditing:] 委托(delegate)给 _NSBindingAdaptor 之前对其使用react,或者尝试在调用层次结构中进一步进行方法混合。除此之外,您还可以将代理对象委托(delegate)给 RLMObject 并在必要时启动写入事务。

或者,根据您需要传播写入事务结果的时间,您可以在呈现 View 并提交 View 时提前启动写入事务,例如当用户在 UI 中点击保存时。

关于macos - Realm 在编辑操作中可以与 Cocoa Binding 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487685/

相关文章:

macos - 操作系统: Unable to open mysql workbench main window after closing it

ios - 主视图下的半隐藏 View 显示在 alpha segue 中

swift - Realm 相同线程的NotificationToken观察commitWrite(没有通知: [token])

macos - 在基于 View 的 NSTableView 中,NSTextField 被 NSImageView 裁剪

c++ - _ctermid.h : No such file or directory error on mac without Xcode

macos - 沙箱用户: This Apple ID has not yet been used with the App Store

ios - Realm:带有属性聚合的查询

ios - 使用 cocoapods 时 Realm.io 构建错误 `use_frameworks!`

javascript - 如何修复 Chrome 浏览器扩展中的 Native Host Has Exited 错误 -- Native Messaging

objective-c - 通过对两个参数进行加权对 NSArray 进行排序