c# - 解决 ThreadSafeReference 时领域崩溃
问题描述
在尝试从单独的线程取回结果时,我的应用程序只是停止工作并退出,有了可用信息,我已经能够编写此代码
public async override Task<IQueryable<Entry>> FindAsync(string inputText)
{
var resultingEntries = await Task.Run(() =>
{
// The heavy stuff starts here, in anew thread.
using (var realm = Realm.GetInstance(DbConfig))
{
// All data
var bgHaystack = realm.All<Entry>();
// Because realm doesn't support some of the LINQ operations on not stored fields (Content)
// the set of entries is converted to a IEnumerable.
IEnumerable<Entry> subset = bgHaystack;
// This is where the search gets actually done
subset = subset.Where(entry => entry.Content.ToLower().StartsWith(inputText));
// Extracts ids
var foundEntryIds = ExtractIdsFromEntries(subset);
// Select entries
var foundEntries = FindingManyMatches(bgHaystack, foundEntryIds.ToArray());
return ThreadSafeReference.Create(foundEntries);
}
});
var results = Realm.GetInstance(DbConfig).ResolveReference(resultingEntries);
return results;
}
一切都很好,除了倒数第二行我想得到结果对象,你知道这里出了什么问题以及如何解决这个问题吗?
堆栈跟踪:
Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
No native Android stacktrace (see debuggerd output).
=================================================================
Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x7cdcb67c70):0x7cdcb67c60 c0 03 5f d6 c8 02 80 52 e0 03 08 2a c0 03 5f d6 .._....R...*.._.
0x7cdcb67c70 08 00 40 79 09 3d 0e 53 08 3d 00 12 69 01 00 35 ..@y.=.S.=..i..5
0x7cdcb67c80 09 01 13 12 2a 01 00 32 0b fc 5f 48 7f 21 29 6b ....*..2.._H.!)k
0x7cdcb67c90 a1 00 00 54 0a 7c 0b 48 8b ff ff 35 e0 03 1f 2a ...T.|.H...5...*
================================================
05-08 23:09:47.037 D/Mono (25933): Found as 'shared_realm_resolve_object_reference'.=================
Managed Stacktrace:
=================================================================
at <unknown> <0xffffffff>
at NativeMethods:resolve_query_reference <0x00007>
at Realms.SharedRealmHandle:ResolveReference <0x00243>
at Realms.Realm:ResolveReference <0x0008f>
at <FindAsync>d__9:MoveNext <0x0062f>
at MoveNextRunner:InvokeMoveNext <0x000f3>
at System.Threading.ExecutionContext:RunInternal <0x003af>
at System.Threading.ExecutionContext:Run <0x0006b>
at MoveNextRunner:Run <0x00193>
at <>c:<.cctor>b__7_0 <0x0009b>
at <>c__DisplayClass2_0:<Post>b__0 <0x00093>
at RunnableImplementor:Run <0x000bb>
at Java.Lang.IRunnableInvoker:n_Run <0x000c3>
at Android.Runtime.DynamicMethodNameCounter:20 <0x000af>
at Android.Runtime.DynamicMethodNameCounter:20 <0x000e3>
=================================================================
05-08 23:09:47.037 D/Mono (25933): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
05-08 23:09:47.037 D/Mono (25933): Searching for 'shared_realm_resolve_list_reference'.
05-08 23:09:47.037 D/Mono (25933): Probing 'shared_realm_resolve_list_reference'.
05-08 23:09:47.037 D/Mono (25933): Found as 'shared_realm_resolve_list_reference'.
05-08 23:09:47.037 D/Mono (25933): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
05-08 23:09:47.037 D/Mono (25933): Searching for 'shared_realm_resolve_query_reference'.
05-08 23:09:47.037 D/Mono (25933): Probing 'shared_realm_resolve_query_reference'.
05-08 23:09:47.037 D/Mono (25933): Found as 'shared_realm_resolve_query_reference'.
05-08 23:09:47.040 F/libc (25933): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x188 in tid 25933 (panyname.dosham), pid 25933 (panyname.dosham)
05-08 23:09:47.061 D/Mono (25933): DllImport searching in: 'realm-wrappers' ('librealm-wrappers.so').
05-08 23:09:47.062 D/Mono (25933): Searching for 'query_destroy'.
05-08 23:09:47.062 D/Mono (25933): Probing 'query_destroy'.
05-08 23:09:47.062 D/Mono (25933): Found as 'query_destroy'.
05-08 23:09:47.259 I/panyname.dosha(25933): ProcessProfilingInfo new_methods=0 is saved saved_to_disk=0 resolve_classes_delay=8000
解决方案
看起来是个bug,删除
using (var realm = Realm.GetInstance(DbConfig))
解决了这个问题,即
var resultingEntries = await Task.Run(() =>
{
var allEntries = Realm.GetInstance(DbConfig).All<Entry>();
// Because realm doesn't support some of the LINQ operations on not stored fields (Content)
// the set of entries is converted to a IEnumerable.
IEnumerable<Entry> subset = allEntries;
// This is where the search gets actually done
subset = subset.Where(entry => entry.Content.ToLower().StartsWith(inputText));
// Extracts ids
var foundEntryIds = ExtractIdsFromEntries(subset);
// Select entries
var foundEntries = FindingManyMatches(allEntries, foundEntryIds.ToArray());
var foundEntriesCount = foundEntries.Count();
return ThreadSafeReference.Create(foundEntries);
});
var results = Realm.GetInstance(DbConfig).ResolveReference(resultingEntries);
return results;
推荐阅读
- javascript - 除了 .innerHTML 之外,还有哪些其他选项可以将 Javascript 生成的 HTML 插入 DOM?
- html - 减小容器高度时将元素放入其容器中
- node.js - 地图列表在 DynamoDB 查询中返回未定义
- javascript - 仅 forEach 必填字段
- python - 使用全局创建单例对象导致多次初始化
- php - post请求中的括号
- node.js - Node.js - Http get 不会在 Postman 或浏览器中显示来自 MongoDb 查询的数据
- c# - 获取 CollectionView 返回对象列表并放入 xamarin.form 中的模型 IIList
- r - 将 tag_facet 导出到 .tif
- c# - 按键后如何执行操作?