Я столкнулся с утечкой памяти, которая неспособна понять PFB
Трассировка вызовов:
- + 0x196 callq "DYLD-STUB $$ NSManagedObjectContext.fetch(NSFetchRequest) throws → [A]"
- + 0x8c callq "_arrayForceCast ([A]) → [B]"
- + 0xde callq "Collection.map((A.Iterator.Element) throws → A1) throws → [A1]"
- + 0x19e callq "ContiguousArray.reserveCapacity(Int) → ()"
- + 0xaa callq "_ContiguousArrayBuffer.init(uninitializedCount: Int, minimumCapacity: Int) → _ContiguousArrayBuffer"
- + 0x42 callq "ManagedBufferPointer.init(_uncheckedBufferClass: AnyObject.Type, minimumCapacity: Int) → ManagedBufferPointer"
- + 0x0f callq "swift_slowAlloc"
- + 0x04 callq "DYLD-STUB $$ malloc"
- + 0x13 callq "malloc_zone_malloc"
- + 0x8f movzbl 71543 (% rip),% eax
Edit:
Далее я исследовал код, и я узнал, что настоящая утечка возникает, когда я пытаюсь принудительно использовать тип литья [Any] для [AnyObject] в запросе на выборку coredatap >
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: @Джон, Kuba
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.managedObjectКонтактный код
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
}() }