在我的快速应用程序中,我有这种伪代码
堆栈跟踪如下
Fatal Exception: RLMException
0 CoreFoundation 0x1839fbd38 __exceptionPreprocess
1 libobjc.A.dylib 0x182f10528 objc_exception_throw
2 Realm 0x1033e574c -[RLMRealm verifyThread] (RLMRealm.mm:156)
3 Realm 0x103363d50 ___ZN12_GLOBAL__N_115makeBoxedGetterIN5realm10StringDataEEEP11objc_objectm_block_invoke (RLMAccessor.mm:51)
4 Foundation 0x1842e9f70 -[NSObject(NSKeyValueCoding) valueForKey:]
5 Realm 0x10337be98 -[RLMObjectBase valueForKey:] (RLMObjectBase.mm:178)
6 Realm 0x10337d138 -[RLMObjectBase hash] (RLMObjectBase.mm:305)
7 UIFoundation 0x18def3ab8 attributeDictionaryHash
8 Foundation 0x1842ebedc hashProbe
9 Foundation 0x1842f147c -[NSConcreteHashTable rehashAround:]
10 Foundation 0x1842ec080 hashProbe
11 Foundation 0x1842ed51c -[NSConcreteHashTable addObject:]
12 UIFoundation 0x18def4628 -[NSAttributeDictionary newWithKey:object:]
13 Foundation 0x1843082b4 -[NSConcreteMutableAttributedString addAttribute:value:range:]
14 Gate 0x1004ef264 specialized MessageGenerator.replace(receivedMention : String, mentions : [String : User], senderID : String, meID : String, font : UIFont, availableWidth : CGFloat, outputAsText : Bool) -> NSAttributedString? (MessageGenerator.swift:147)
15 Gate 0x1004ef7c8 specialized MessageGenerator.replaceMentionsForChats(originalString : String, mentions : [String : User], senderID : String, meID : String, font : UIFont, availableWidth : CGFloat, outputAsText : Bool) -> NSAttributedString (MessageGenerator.swift:176)
16 Gate 0x1004f05c8 specialized MessageGenerator.getMentionReadyMessage(communicator : Communicator, conversationId : String, receivedString : String, mentions : [Mention], sentByMe : Bool, senderID : String, maxWidthForMention : CGFloat, font : UIFont?, outputAsText : Bool) -> NSAttributedString (MessageGenerator.swift:89)
17 Gate 0x1004f0810 specialized MessageGenerator.prepareForMentionReadyMessage(communicator : Communicator, conversation : ConversationItem, message : Message, payload : TextPayload, maxWidthForMention : CGFloat, font : UIFont?, outputAsText : Bool) -> NSAttributedString? (MessageGenerator.swift:60)
18 Gate 0x100508174 specialized WatchMessageVisitor.extractTextPayload(payload : TextPayload, input : String) -> NSAttributedString (WatchMessageVisitor.swift:62)
19 Gate 0x1005082c8 specialized WatchMessageVisitor.visit(payload : TextPayload) -> () (WatchMessageVisitor.swift:41)
20 Gate 0x1003ecea8 protocol witness for PayloadVisitor.visit(payload : TextPayload) -> () in conformance MessageTextInterpreter (MessageTextInterpreter.swift)
21 Codebase 0x101d27b2c TextPayloadCodebasePayloadProtocol (Payload.swift:93)
22 Gate 0x1003edd38 specialized MessageTextInterpreter.init(communicator : Communicator, conversation : ConversationItem, message : Message?, maxWidthForMention : CGFloat, fontForMention : UIFont?) -> MessageTextInterpreter (MessageTextInterpreter.swift:46)
23 Gate 0x1003780b8 WatchWrapperserialise[[String : Any]]LazyArray (Bootstrap.swift)
24 Gate 0x100378620 WatchWrapperserialise[[String : Any]]LazyArray (Bootstrap.swift:71)
25 Gate 0x100377364 WatchWrapperCodebaseWatchSerializerGateWatchSerializerserialise[[String : Any]]LazyArray (Bootstrap.swift)
26 Codebase 0x101d75740 WatchManagerModule.(updateWatchData() -> ()).(closure #1) (WatchManagerModule.swift:57)
27 Codebase 0x101d78e70 partial apply for thunk (WatchManagerModule.swift)
28 RxSwift 0x105331d5c ObservableTypesubscribeDisposable(A.E) -> ()?onNext first-element-marker (Error) -> ()?onError() -> ()?onCompleted (ObservableType+Extensions.swift:104)
29 RxSwift 0x105331a80 ObservableTypesubscribeDisposable(A.E) -> ()?onNext first-element-marker (Error) -> ()?onError() -> ()?onCompleted (ObservableType+Extensions.swift)
30 RxSwift 0x105331bec ObservableTypesubscribeDisposable(A.E) -> ()?onNext first-element-marker (Error) -> ()?onError() -> ()?onCompleted (ObservableType+Extensions.swift)
31 RxSwift 0x1052dc2b8 AnonymousObserver.onCore(Event<A>) -> () (AnonymousObserver.swift:24)
32 RxSwift 0x1053345c0 ObserverBase.on(Event<A>) -> () (ObserverBase.swift:21)
33 RxSwift 0x10533487c ObserverBase<A>RxSwiftObserverTypeRxSwift<A>RxSwift (ObserverBase.swift)
34 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
35 RxSwift 0x10530bcb0 AnonymousObservableSink.on(Event<A.E>) -> () (Create.swift:50)
36 RxSwift 0x10530be40 AnonymousObservableSink<A>RxSwiftObserverType (Create.swift)
37 RxSwift 0x105334c08 partial apply (ObserverType.swift)
38 RxSwift 0x1052dc498 AnyObserver.on(Event<A>) -> () (AnyObserver.swift:39)
39 RxSwift 0x1052dc508 AnyObserver<A>RxSwiftObserverTypeRxSwift<A>RxSwift (AnyObserver.swift)
40 RxSwift 0x105334a04 ObserverType.onNext(A.E) -> () (ObserverType.swift:28)
41 Codebase 0x101d24ba4 static Observable.(fromCallable(action : () throws -> A?) -> Observable<A>).(closure #1)LazyArray
42 RxSwift 0x10530bf5c AnonymousObservable.run<A where ...> (A1, cancel : Cancelable) -> (sink : Disposable, subscription : Disposable) (Create.swift:60)
43 RxSwift 0x10533f95c Producer.subscribe<A where ...> (A1) -> Disposable (Producer.swift:19)
44 RxSwift 0x105331958 ObservableTypesubscribeDisposable(A.E) -> ()?onNext first-element-marker (Error) -> ()?onError() -> ()?onCompleted (ObservableType+Extensions.swift:124)
45 Codebase 0x101d7568c WatchManagerModule.updateWatchData() -> () (WatchManagerModule.swift:58)
46 Codebase 0x101d7614c WatchSyncCandidate.onComplete() -> () (WatchManagerModule.swift:170)
47 Codebase 0x101d50354 SyncRunner.run(communicator : Communicator, entities : [SyncHandler]) -> Observable<()>Communicator (SyncRunner.swift:86)
48 Codebase 0x101d4f1f8 SyncRunner.run(communicator : Communicator, entities : [SyncHandler]) -> Observable<()>Communicator (SyncRunner.swift)
49 RxSwift 0x10531b15c ObservableTypedoObservable<A.E>(A.E) throws -> ()?onNext first-element-marker (Error) throws -> ()?onError() throws -> ()?onCompleted() -> ()?onSubscribe (Do.swift:32)
50 RxSwift 0x10531b3d0 ObservableTypedoObservable<A.E>(A.E) throws -> ()?onNext first-element-marker (Error) throws -> ()?onError() throws -> ()?onCompleted() -> ()?onSubscribe (Do.swift)
51 RxSwift 0x10531b514 DoSink.on(Event<A.E>) -> () (Do.swift:51)
52 RxSwift 0x10531b89c DoSink<A>RxSwiftObserverType (Do.swift)
53 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
54 RxSwift 0x105367e40 SwitchIfEmptySinkIter.on(Event<A.E>) -> () (SwitchIfEmpty.swift:96)
55 RxSwift 0x10536802c SwitchIfEmptySinkIter<A>RxSwiftObserverType (SwitchIfEmpty.swift)
56 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
57 RxSwift 0x10530bc48 AnonymousObservableSink.on(Event<A.E>) -> () (Create.swift:53)
58 RxSwift 0x10530be40 AnonymousObservableSink<A>RxSwiftObserverType (Create.swift)
59 RxSwift 0x105334c08 partial apply (ObserverType.swift)
60 RxSwift 0x1052dc498 AnyObserver.on(Event<A>) -> () (AnyObserver.swift:39)
61 RxSwift 0x1052dc508 AnyObserver<A>RxSwiftObserverTypeRxSwift<A>RxSwift (AnyObserver.swift)
62 RxSwift 0x105334a90 ObserverType.onCompleted() -> () (ObserverType.swift:33)
63 Codebase 0x101d24f88 specialized static Observable.(execute(action : () throws -> ()) -> Observable<A>).(closure #1) (Observable+Ext.swift:36)
64 RxSwift 0x10530bf5c AnonymousObservable.run<A where ...> (A1, cancel : Cancelable) -> (sink : Disposable, subscription : Disposable) (Create.swift:60)
65 RxSwift 0x10533f95c Producer.subscribe<A where ...> (A1) -> Disposable (Producer.swift:19)
66 RxSwift 0x105367a78 SwitchIfEmptySink.on(Event<A.E>) -> () (SwitchIfEmpty.swift:76)
67 RxSwift 0x105367cec SwitchIfEmptySink<A>RxSwiftObserverType (SwitchIfEmpty.swift)
68 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
69 RxSwift 0x10532aa08 MergeSink.on(Event<A>) -> () (Merge.swift:529)
70 RxSwift 0x10532b86c MergeSink<A, B, C>RxSwiftObserverType (Merge.swift)
71 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
72 RxSwift 0x10532b6e4 MergeSinkIter.on(Event<B.E>) -> () (Merge.swift:529)
73 RxSwift 0x10532b74c MergeSinkIter<A, B, C>RxSwiftObserverType (Merge.swift)
74 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
75 RxSwift 0x105313678 DefaultIfEmptySink.on(Event<A.E>) -> () (DefaultIfEmpty.swift:46)
76 RxSwift 0x1053138f0 DefaultIfEmptySink<A>RxSwiftObserverType (DefaultIfEmpty.swift)
77 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
78 RxSwift 0x10532aa08 MergeSink.on(Event<A>) -> () (Merge.swift:529)
79 RxSwift 0x10532b86c MergeSink<A, B, C>RxSwiftObserverType (Merge.swift)
80 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
81 RxSwift 0x10532b6e4 MergeSinkIter.on(Event<B.E>) -> () (Merge.swift:529)
82 RxSwift 0x10532b74c MergeSinkIter<A, B, C>RxSwiftObserverType (Merge.swift)
83 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
84 RxSwift 0x105363ac4 SubscribeOnSink.on(Event<A.E>) -> () (SubscribeOn.swift:44)
85 RxSwift 0x105364530 SubscribeOnSink<A, B>RxSwiftObserverType (SubscribeOn.swift)
86 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
87 RxSwift 0x1053140e0 DeferredSink.on(Event<A.E>) -> () (Deferred.swift:47)
88 RxSwift 0x1053143ec DeferredSink<A, B>RxSwiftObserverType (Deferred.swift)
89 RxSwift 0x10535e60c Sink.forwardOn(Event<A.E>) -> () (Sink.swift:36)
90 RxSwift 0x10530bc48 AnonymousObservableSink.on(Event<A.E>) -> () (Create.swift:53)
91 RxSwift 0x10530be40 AnonymousObservableSink<A>RxSwiftObserverType (Create.swift)
92 RxSwift 0x105334c08 partial apply (ObserverType.swift)
93 RxSwift 0x1052dc498 AnyObserver.on(Event<A>) -> () (AnyObserver.swift:39)
94 RxSwift 0x1052dc508 AnyObserver<A>RxSwiftObserverTypeRxSwift<A>RxSwift (AnyObserver.swift)
95 RxSwift 0x105334a90 ObserverType.onCompleted() -> () (ObserverType.swift:33)
96 Codebase 0x101d24f88 specialized static Observable.(execute(action : () throws -> ()) -> Observable<A>).(closure #1) (Observable+Ext.swift:36)
97 RxSwift 0x10530bf5c AnonymousObservable.run<A where ...> (A1, cancel : Cancelable) -> (sink : Disposable, subscription : Disposable) (Create.swift:60)
98 RxSwift 0x10533f95c Producer.subscribe<A where ...> (A1) -> Disposable (Producer.swift:19)
99 RxSwift 0x1053313e4 Observable<A>RxSwiftObservableTypeRxSwift<A>RxSwift (Observable.swift)
100 RxSwift 0x105313fb4 DeferredSink.run() -> Disposable (Deferred.swift:37)
101 RxSwift 0x1053144c8 Deferred.run<A where ...> (A1, cancel : Cancelable) -> (sink : Disposable, subscription : Disposable) (Deferred.swift:72)
102 RxSwift 0x10533feb0 Producer.(subscribe<A where ...> (A1) -> Disposable).(closure #1) (Producer.swift:27)
103 RxSwift 0x10530c67c specialized specialized CurrentThreadScheduler.schedule<A> (A, action : (A) -> Disposable) -> Disposable (CurrentThreadScheduler.swift:66)
104 RxSwift 0x10533faa8 Producer.subscribe<A where ...> (A1) -> Disposable (Producer.swift:30)
105 RxSwift 0x1053313e4 Observable<A>RxSwiftObservableTypeRxSwift<A>RxSwift (Observable.swift)
106 RxSwift 0x105363f98 SubscribeOnSink.(run() -> Disposable).(closure #1) (SubscribeOn.swift:59)
107 RxSwift 0x1053695fc thunk (Take.swift)
108 RxSwift 0x105318ce8 specialized DispatchQueueConfiguration.(schedule<A> (A, action : (A) -> Disposable) -> Disposable).(closure #1) (DispatchQueueConfiguration.swift:34)
109 RxSwift 0x1053191e8 partial apply for DispatchQueueConfiguration.(schedule<A> (A, action : (A) -> Disposable) -> Disposable).(closure #1) (DispatchQueueConfiguration.swift)
110 RxSwift 0x10530b4a8 thunk (ConcurrentMainScheduler.swift)
111 libdispatch.dylib 0x183381088 _dispatch_call_block_and_release
112 libdispatch.dylib 0x183381048 _dispatch_client_callout
113 libdispatch.dylib 0x1833893d4 _dispatch_continuation_pop$VARIANT$mp
114 libdispatch.dylib 0x183387d88 _dispatch_async_redirect_invoke$VARIANT$mp
115 libdispatch.dylib 0x18338e1c8 _dispatch_root_queue_drain
116 libdispatch.dylib 0x18338df10 _dispatch_worker_thread3
117 libsystem_pthread.dylib 0x183627130 _pthread_wqthread
118 libsystem_pthread.dylib 0x183626c30 start_wqthread
来自下面的代码段
let user = realm.object(ofType: User.self, forPrimaryKey: $0.identifier)
let name = "@\(user.nickname)"
let text = NSMutableAttributedString(string: " " + name + " ")
let range = NSRange(location: 0, length: text.length)
text.addAttribute(NSBackgroundColorAttributeName, value: #colorLiteral(red:0.3058823529, green:0.6156862745, blue:0.9254901961, alpha:1), range: range)
text.addAttribute(NSForegroundColorAttributeName, value: #colorLiteral(red:1, green:1, blue:1, alpha:1), range: range)
text.addAttribute(NSFontAttributeName, value: font, range: range)
是什么让我想知道为什么Realm代码在NSMutableAttributedString内部代码中执行,其中正在使用的是String的名称。
其次所有代码实际上都是在同一个线程内执行,而不是主线程。
在应用程序中,我们确实有一个或两个线程保留从自己的Realm线程实例中检索的对象。
此崩溃是通过Crashlytic报告的,我无法重现该方案,但崩溃仍在发生。
希望有人可以了解实际情况。