ios - 用于获取 NSManagedObject 的获取请求的 NSPredicate 上的内存泄漏
<p><p>根据仪器,我在 NSPredicate 上有内存泄漏。如何避免这种内存泄漏?出了什么问题?</p>
<p>代码如下:
注意:</p>
<p>默认管理器是单例</p>
<p>self.editingContext 是具有父上下文(主上下文)的子上下文</p>
<p>这个函数(JobType)的输出是在父 NSManagedObject 中使用的 NSManagedObject(例如 job.type = jobType)</p>
<pre><code>func defaultJobType() -> JobType?
{
let fetchRequest = NSFetchRequest(entityName: JobType.entityName());
let predicate = NSPredicate(format: "jobTypeID = %@ && (archived = nil || archived = 0)", DefaultManager.instance.defaultValues!.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;
}
</code></pre>
<p>函数defaultJobType()的调用者</p>
<pre><code>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;
}
</code></pre>
<p>并且在Objective C ViewController中使用了createDefaultJob()。</p>
<p>请帮忙</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我也遇到了同样的问题。 iOS 9 之前是没有的。还有雷达<a href="https://openradar.appspot.com/23025446" rel="noreferrer noopener nofollow">here</a> </p>
<p>我设法通过重新配置我的核心数据堆栈来解决它。基本上,当我使用父托管上下文(主)和嵌套的子托管上下文时,我遇到了问题:</p>
<p>我将父上下文和子上下文更改为两个(非嵌套)独立的上下文,共享同一个持久性存储协调器。我手动合并更改如下:</p>
<pre><code>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)
}
}
</code></pre>
<p>我运行泄漏仪器,泄漏消失了。希望这会有所帮助。</p></p>
<p style="font-size: 20px;">关于ios - 用于获取 NSManagedObject 的获取请求的 NSPredicate 上的内存泄漏,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33272197/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33272197/
</a>
</p>
页:
[1]