gpt4 book ai didi

ios - MagicalRecords 通过图像获取在后台线程中持续存在?

转载 作者:可可西里 更新时间:2023-11-01 04:42:10 26 4
gpt4 key购买 nike

我有以下代码:

dispatch_async(dispatch_get_main_queue(), ^{
NSManagedObjectContext *localContext = [NSManagedObjectContext contextForCurrentThread];

Item *newItem = [Item createInContext:localContext];
newItem.title = NULL_TO_NIL([itemJson valueForKey:@"title"]);
newItem.image_url = NULL_TO_NIL([itemJson valueForKey:@"image_url"]);
newItem.order_id = @([[self largestOrderId] intValue] + 1);

NSURL *url = [NSURL URLWithString:newItem.image_url];
NSData *data = [[NSData alloc] initWithContentsOfURL: url];
if (data == nil) {
NSLog(@"Image data is nil from %@", url);
} else {
NSLog(@"Image fetched in saveItemFromJson for cid:%@ order_id:%@", newItem.cid, newItem.order_id);
newItem.image = [UIImage imageWithData:data];
}

if (![localContext hasChanges]) {
NSLog(@"No local change detected. Quitting");
return;
}

[localContext saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (!success)
NSLog(@"Error: %@", [error localizedDescription]);
else
NSLog(@"Item persisted for cid:%@ order_id:%@", newItem.cid, newItem.order_id);
}];
});

我似乎得到了很多以下内容:

2013-02-13 18:55:47.404 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Saving <NSManagedObjectContext (0x8386a90): *** DEFAULT ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.404 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Parents? 1
2013-02-13 18:55:47.404 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Synchronously? 0
2013-02-13 18:55:47.497 Giordano.iPhone[13956:c07] Image fetched in saveItemFromJson for cid:7218 order_id:10
2013-02-13 18:55:47.497 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Saving <NSManagedObjectContext (0x8386a90): *** DEFAULT ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.498 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Parents? 1
2013-02-13 18:55:47.498 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8386a90) → Save Synchronously? 0
2013-02-13 18:55:47.499 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8386a90) Context DEFAULT is about to save. Obtaining permanent IDs for new 10 inserted objects
2013-02-13 18:55:47.501 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) → Saving <NSManagedObjectContext (0x8385aa0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.502 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) → Save Parents? 0
2013-02-13 18:55:47.502 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) → Save Synchronously? 1
2013-02-13 18:55:47.502 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x8385aa0) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 10 inserted objects
2013-02-13 18:55:47.505 Giordano.iPhone[13956:c07] __70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x8385aa0) → Finished saving: <NSManagedObjectContext (0x8385aa0): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
2013-02-13 18:55:47.505 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING

错误信息如下:

2013-02-13 18:55:47.511 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.512 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.512 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.512 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING
2013-02-13 18:55:47.513 Giordano.iPhone[13956:c07] -[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x8385aa0) NO CHANGES IN ** BACKGROUND SAVING (ROOT) ** CONTEXT - NOT SAVING


2013-02-13 18:55:47.515 Giordano.iPhone[13956:c07] Error: (null)
2013-02-13 18:55:47.515 Giordano.iPhone[13956:c07] Error: (null)
2013-02-13 18:55:47.515 Giordano.iPhone[13956:c07] Error: (null)

我避免使用 saveInBackgroundWithBlock 因为它已被弃用(文档需要更新?)

对我的代码有什么问题有什么想法吗?

更新

我的团队认为 MagicalRecord 现在的问题太多了。我们已经完全将我们的代码从 MR 迁移回 CoreData。感谢您的关注。

最佳答案

我遇到了同样的问题!只有当我在没有 MR 的情况下自己手动保存上下文时才修复。

这是我的解决方案:

NSManagedObject+MyCategory.h

+ (void)saveDataInBackgroundWithBlock:(void(^)(NSManagedObjectContext *localContext))saveBlock
completion:(void(^)(void))completion;

+ (NSManagedObjectContext *)newMergableBackgroundThreadContext;

- (void)saveWithCompletion:(void(^)(void))completion;

.m

+ (NSManagedObjectContext *)newMergableBackgroundThreadContext {
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
context.parentContext = [self mainThreadContext];
[context.userInfo setObject:[NSNumber numberWithInteger:VKCoreDataManagedObjectContextIDTempBackground]
forKey:@"contextID"];
[context.userInfo setObject:kVKCoreDataManagedObjectContextBackgroundTemp
forKey:@"contextDebugName"];
VKDLog(@"* New mergable backround context created! *");
return context;
}

+ (void)saveDataInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))saveBlock completion:(void (^)(void))completion {
NSManagedObjectContext *tempContext = [self newMergableBackgroundThreadContext];
[tempContext performBlock:^{

if (saveBlock) {
saveBlock(tempContext);
}

if ([tempContext hasChanges]) {
[tempContext saveWithCompletion:completion];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion();
}
});
}
}];
}

- (void)saveWithCompletion:(void(^)(void))completion {
[self performBlock:^{
NSError *error = nil;
if ([self save:&error]) {
NSNumber *contextID = [self.userInfo objectForKey:@"contextID"];
if (contextID.integerValue == VKCoreDataManagedObjectContextIDMainThread) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion();
}
});
}
[[self class] logContextSaved:self];
if (self.parentContext) {
[self.parentContext saveWithCompletion:completion];
}
} else {
[VKCoreData handleError:error];
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion();
}
});
}
}];
}

这里是使用示例:

[NSManagedObjectContext saveDataInBackgroundWithBlock:^(NSManagedObjectContext *localContext){
// do your stuff with local context
} completion:^{
// handle completion, update UI or something
}];

关于ios - MagicalRecords 通过图像获取在后台线程中持续存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867102/

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