无法从另一个线程删除Realm数据库对象(即使在搜索之后) [英] Can't delete Realm database objects from another thread (even after searching)
问题描述
我在此线程中遇到与发布者类似的问题: 无法删除领域数据库中的对象
I'm having a similar problem as the poster in this thread: Unable to delete an object in a realm database
我有一个名为"syncQueue"的队列中的搜索创建的RLMObject子类数组.这是一堆需要发送到服务器的诊断对象.
I have an array of RLMObject subclasses created from a search on a queue called 'syncQueue'. It's a bunch of Diagnostic objects that need to be sent up to the server.
使用 Google将这些对象打包并发送到服务器iOS客户端库.库返回该对象已成功上传后,我想将它们从Realm中删除.我从Realm中获取了一些示例.
These objects are packaged up and sent up to the server using the Google Client Library for iOS. Once the library returns that the objects have been successfully uploaded, I want to delete them out of Realm. I grab some samples out of Realm.
NSArray *samples = [someClass getMySamplesFromRealm];
所以现在我有一系列样本. ^^这发生在使用自己线程的'syncQueue'上.仍然在同一线程上,我们打包并上传示例.因此,有一个类似于以下内容的回调块:
So now I have an array of samples. ^^This happens on the 'syncQueue', which uses its own thread. Still on the same thread, we package and upload the samples. So there's a callback block that looks something like this:
GTLServiceTicket *ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error)
{
[syncHandler removeTicket:ticket];
if( error )
{
// Handle Error
}
else
{
[diagDataAccessManager deleteObjects:samples];
}
}];
这是删除这些对象的代码.
Here is the code that deletes these objects.
1 - (void)deleteObjects:(NSArray *)samples
2 {
3 if (samples.count > 0)
4 {
5 [[RLMRealm defaultRealm] beginWriteTransaction];
6 for (Diagnostic *diagnostic in samples)
7 {
8 Diagnostic *diagnosticToDelete = [Diagnostic objectForPrimaryKey:diagnostic.primaryId];
9 [[RLMRealm defaultRealm] deleteObject:diagnosticToDelete];
10 }
11 [[RLMRealm defaultRealm] commitWriteTransaction];
12 }
13
14 }
因此,我尝试先进行搜索,然后再执行删除操作.但是...
So I'm trying to do a search before I do a delete. However...
在第8行,我得到一个例外:从错误的线程访问领域" 现在,我认为这是因为当我运行Diagnostic objectForPrimaryKey:
时,它正在尝试使用与*diagnostic
对象,该对象是在另一个线程上创建的.
On line 8, I get an exception: "Realm accessed from incorrect thread" Now, I assume this is because when I run Diagnostic objectForPrimaryKey:
, it's trying to use the Realm associated with the *diagnostic
object, which was created on another thread.
我的问题是:如果我无法看清它们是哪些这些特定对象,我该如何删除这些特定对象?
My question is: how on Earth can I delete these specific objects, if I can't look at them to figure out which specific objects they are?
任何澄清/建议将不胜感激.
Any clarification/advice would be much appreciated.
推荐答案
您最好的方法是将主键传递给新线程,而不是对象.
Your best will probably be to pass the primary keys to the new thread, rather than the objects.
这篇关于无法从另一个线程删除Realm数据库对象(即使在搜索之后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!