编辑:
我进一步研究了代码,发现真正的泄漏是当我尝试在coredata获取请求中强制将类型[Any]强制转换为[AnyObject]时
func fetchEntity<T: NSManagedObject>(entityClass:T.Type,setPredicate:NSPredicate?) -> [AnyObject]
{
let entityName = NSStringFromClass(entityClass)
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: entityName)
fetchRequest.predicate = setPredicate
fetchRequest.returnsObjectsAsFaults = false
var result:[AnyObject] = []
do
{
result = try cdh.managedObjectContext.fetch(fetchRequest) --> right here is the leak, when i cast the return object of [Any] to [AnyObject]
}catch let error as NSError
{
debugPrint("error in fetchrequest is",error)
result = []
}
return result
}
编辑:
@乔恩·库巴
Model.getEntities(entityType: EX_TEACHER.self, completion: {[unowned self] entityobjects in
self.teacherList = entityobjects
})
//在模型类中
class func getEntities<T: NSManagedObject>(entityType: T.Type,completion: ([AnyObject]) -> Void)
{
let teacherList = coreDataOperation.fetchEntity(entityClass: entityType, setPredicate: nil)
completion(teacherList)
}
//cdh.managedObjectContext代码
lazy var cdh:CoreDataStore = {
let cdh = CoreDataStore()
return cdh
}()
class CoreDataStore: NSObject{
lazy var managedObjectContext: NSManagedObjectContext = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}() }
最佳答案
经过大量的编译和谷歌搜索。我放弃并下载了新的xcode版本8.3。 (之前为8.1)
然后我重新测试了该项目,然后猜猜是什么.....?
没有泄漏了。好吧,我们可以猜测这是xcode 8.1版本的问题。
编辑:
调试
已解决的问题
适用于macOS和iOS Simulator的Memory Debugger(内存调试器)修复了Swift类的错误内存泄漏报告,其中Swift类包含枚举类型的字段或从某些Objective-C框架类继承的类。 (27932061)
对于iOS,watchOS和tvOS应用程序,仍然可能出现虚假报告。请参阅下面的已知问题中的(29335813)。
已知问题
当调试包含枚举类型的字段或从某些Objective-C框架类继承的类的Swift类时,iOS,watchOS和tvOS应用程序的Memory Debugger可能报告错误的内存泄漏。 (29335813)
apple Xcode 8.3 release notes