I am facing a memory leak which am unable to understand PFB
Call trace:
Edit:
I Further investigated the code and i found out that the real leak is when i try to force cast type [Any] to [AnyObject] in coredata fetch request
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
}
EDIT: @Jon,Kuba
Model.getEntities(entityType: EX_TEACHER.self, completion: {[unowned self] entityobjects in
self.teacherList = entityobjects
})
// in model class
class func getEntities<T: NSManagedObject>(entityType: T.Type,completion: ([AnyObject]) -> Void)
{
let teacherList = coreDataOperation.fetchEntity(entityClass: entityType, setPredicate: nil)
completion(teacherList)
}
// cdh.managedObjectContext code
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
}() }
After a lot of compilation and google search. i gave up and downloaded a new xcode version 8.3. (previous was 8.1)
Then i retested the project and guess what.....? no leaks anymore. Well we can guess that it was xcode 8.1 version issues.
EDIT:
Debugging
Resolved Issues The Memory Debugger for macOS and the iOS Simulator fixes reporting of false memory leaks for Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (27932061) False reporting can still occur for iOS, watchOS, and tvOS applications. See (29335813) in Known Issues, below.
Known Issues The Memory Debugger for iOS, watchOS, and tvOS applications can report false memory leaks when debugging Swift classes containing either fields of type enum, or classes that inherit from certain Objective-C framework classes. (29335813)
apple Xcode 8.3 release notes
Try this code
do
{
var result: AnyObject = try cdh.managedObjectContext.fetch(fetchRequest)
let resultMirror = Mirror(reflecting: result)
print(resultMirror.subjectType) //gives you the return type from coredata
}catch let error as NSError
{
debugPrint("error in fetchrequest is",error)
result = []
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With