gpt4 book ai didi

ios - 从错误线程访问的 Realm

转载 作者:行者123 更新时间:2023-11-29 00:26:01 25 4
gpt4 key购买 nike

我写了下面的方法来处理文件的下载。

- (void)downloadFileFromURL:(NSURL *)url {
if (url != nil) {
if ([url pathExtension] != nil) {
RLMRealm *realm = [Utilities getRealm];

RLMResults<Download *> *results = [[Download allObjectsInRealm:realm] sortedResultsUsingKeyPath:@"id" ascending:NO];
Download *oldestDownload = [results firstObject];

Download *download = [[Download alloc] init];
download.id = oldestDownload.id+1;
download.name = [url path];
download.type = @"html";
download.state = DownloadStateNew;
download.token = [Utilities generateToken];
download.url = [url absoluteString];

[realm beginWriteTransaction];
[realm addObject:download];
self.current = download;
[realm commitWriteTransaction];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:download.name];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"application/json", @"image/png", nil];
[manager GET:[NSString stringWithFormat:@"%@", url]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSData *data = [[NSData alloc] initWithData:responseObject];
[data writeToFile:path atomically:YES];
NSLog(@"successful download to %@", path);

RLMRealm *realm = [Utilities getRealm];
[realm beginWriteTransaction];
self.current.state = DownloadStateDone;
[realm commitWriteTransaction];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);


RLMRealm *realm = [Utilities getRealm];
[realm beginWriteTransaction];
self.current.state = DownloadStateCancelled;
[realm commitWriteTransaction];
}];
}
}
}

但是,这会不断抛出一个错误,表明从不正确的线程访问了 Realm。我不知道是什么原因造成的。有什么想法吗?

最佳答案

从 AFNetworking 代码来看,successfailure block 都将在与最初用于启动请求。

为了保证 ACID 合规性,Realm 对象是线程限制的,如果您尝试将一个对象传递给另一个线程,然后访问它的任何属性,这将触发您看到的异常。

我们最近在 Realm Objective-C 和 Realm Swift 中添加了一项功能,可以传递表示 Realm 对象的线程安全引用,然后您可以使用这些引用在新线程上查询相同的对象:

// Do work with `self.current`

RLMThreadSafeReference *currentReference = [RLMThreadSafeReference referenceWithThreadConfined:self.current];

AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];

//Configure `manager`

[manager GET:[NSString stringWithFormat:@"%@", url]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
MyCurrentClass *threadCurrent = [RLMRealm resolveThreadSafeReference:currentReference];
[threadCurrent.realm beginWriteTransaction];
threadCurrent.state = DownloadStateDone;
[threadCurrent.realm commitWriteTransaction];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Handle `threadReference` here too if needed
}];

关于ios - 从错误线程访问的 Realm ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43004235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com