NSPredicate上的内存泄漏,该内存泄漏用于获取请求以获取NSManagedObject [英] Memory leak on NSPredicate that used in fetch request to fetch NSManagedObject
问题描述
根据该仪器,我在NSPredicate上发生内存泄漏.如何避免这种内存泄漏?出了什么问题?
According to the instrument I have memory leak on the NSPredicate. How can I avoid this memory leak? what went wrong?
代码如下: 注意:
默认管理器为Singleton
Default Manager is Singleton
self.editingContext是具有父上下文(主上下文)的子上下文
self.editingContext is a child context that has a parent context (main context)
此函数(JobType)的输出是在父NSManagedObject中使用的NSManagedObject(例如job.type = jobType)
The output of this function (JobType) is NSManagedObject that use in the parent NSManagedObject (e.g job.type = jobType)
func defaultJobType() -> JobType?
{
let fetchRequest = NSFetchRequest(entityName: JobType.entityName());
let predicate = NSPredicate(format: "jobTypeID = %@ && (archived = nil || archived = 0)", DefaultManager.instance.defaultValues[DJobType]!.uppercaseString);
fetchRequest.predicate = predicate;
do{
return try self.editingContext?.executeFetchRequest(fetchRequest).first as? JobType;
}
catch let error as NSError
{
Logger.logApplicationError("Error in getting default job Type", detailMessage: error.localizedDescription);
}
catch
{
Logger.logApplicationError("Error in getting default job Type", detailMessage: "No detail error found");
}
return nil;
}
函数defaultJobType()的调用者
The caller of the function defaultJobType()
func createDefaultJob() -> Job
{
let job = Job.MR_createInContext(self.editingContext!) as! Job;
job.assignedUserID = LoginManager.sharedInstance().currentUserID;
job.createdBy = LoginManager.sharedInstance().currentUserID;
job.createdOn = NSDate();
job.lastModifiedByUser = LoginManager.sharedInstance().currentUserID;
if let defaultCallout = self.servicesDefault.defaultCalloutFee() {
if let jobCalloutFee = defaultCallout.createJobCalloutFee()
{
job.addJobCalloutFeesObject(jobCalloutFee);
}
}
job.type = self.servicesDefault.defaultJobType();
return job;
}
然后在Objective C ViewController中使用createDefaultJob().
And createDefaultJob() is used in Objective C ViewController.
请帮助
推荐答案
我也遇到了同样的问题.在iOS 9之前就不存在了.还有一个雷达此处
I just encountered the same issue as well. It was not there before iOS 9. There is also a radar here
我设法通过重新配置核心数据堆栈来解决此问题.基本上,当我使用父托管环境(main)和嵌套子托管环境时,我遇到了问题:
I managed to solve it by re-configuring my core data stack. Basically, I was running into the problem when I was using a parent managed context (main) and a nested child managed context:
我将父级和子级上下文更改为共享同一持久性存储协调器的两个(未嵌套)单独的上下文.我手动合并了更改,如下所示:
I changed my parent and child context to two (not nested) separate contexts sharing the same persistence store coordinator. I manually merged the changes as follows:
let moc = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
moc.persistentStoreCoordinator = coordinator
let privateMoc = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMoc.persistentStoreCoordinator = coordinator
NSNotificationCenter.defaultCenter().addObserverForName(NSManagedObjectContextDidSaveNotification, object: moc, queue: nil) { notification in
privateMoc.performBlock {
privateMoc.mergeChangesFromContextDidSaveNotification(notification)
}
}
我运行泄漏仪器,泄漏消失了.希望这会有所帮助.
I run the leaks instrument and the leaks are gone. Hope this helps.
这篇关于NSPredicate上的内存泄漏,该内存泄漏用于获取请求以获取NSManagedObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!