根据该仪器,我在NSPredicate上有内存泄漏。我怎样才能避免这种内存泄漏?出了什么问题?
代码如下: 注意:
默认管理器是Singleton
self.editingContext是具有父上下文(主上下文)
的子上下文此函数(JobType)的输出是在父NSManagedObject中使用的NSManagedObject(例如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()
的调用者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;
}
createDefaultJob()用于Objective C ViewController。
请帮忙
答案 0 :(得分:2)
我刚刚遇到了同样的问题。在iOS 9之前它不存在。还有一个雷达here
我设法通过重新配置我的核心数据堆栈来解决它。基本上,当我使用父管理上下文(main)和嵌套子管理上下文时,我遇到了问题:
我将父和子上下文更改为共享相同持久性存储协调器的两个(非嵌套)单独上下文。我按如下方式手动合并了更改:
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)
}
}
我运行泄漏仪器,泄漏消失了。希望这会有所帮助。