###前提・実現したいこと
現在、Swift3でRealmに複数の画像データを登録する処理を行っているのですが、Realmへの書き込み時にメモリリークが起きてしまいます。
###該当のソースコード
Swift
1 2- ViewController 3 4func imageDonwlooad() { 5 dispatchGroup.enter() 6 7 dispatchQueue.async(group: dispatchGroup, execute: { [weak self] () -> () in 8 connection.imageConnection( 9 completion: { [weak self] (err: Error?) in 10 dispatchGroup.leave() 11 } 12 ) 13 }) 14} 15 16 17- URLSessionClass 18 19 func connection(requestUrl: String, completion:@escaping (ConnectionStatus, Error?, Data?) -> ()) { 20 21 let config: URLSessionConfiguration = .default 22 let session: URLSession = URLSession(configuration: config) 23 24 let request: URLRequest = URLRequest(url: URL(string: requestUrl)!, cachePolicy: URLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10) 25 26 let task: URLSessionDataTask = session.dataTask( 27 with: request) { (data: Data?, res: URLResponse?, err: Error?) in 28 29 if let resultError = err { 30 session.invalidateAndCancel() 31 32 completion(.failure, resultError, nil) 33 } 34 else { 35 36 session.invalidateAndCancel() 37 38 if let resultData = data { 39 completion(.success, nil, resultData) 40 } 41 else { 42 completion(.failure, error, nil) 43 } 44 } 45 } 46 task.resume() 47 } 48 49 50- RealmRegistClass 51 52func registImgData(objects: [Object]) throws { 53 54 // Realmを取得 55 let realm: Realm = try Realm() 56 57 // オブジェクトをRealmに追加する 58 for object in objects { 59 try realm.write { 60 realm.add(object) 61 } 62 } 63 } 64 65 // 下記の追加方法も試してみました 66 // try realm.write { 67 // realm.add(objects) 68 // } 69}
###試したこと
画像データをダウンロードしない場合で試したところ、メモリリークは出なかったです。
また、容量が軽い画像データでもメモリリークは出ないです。
アプリ自体はクラッシュはしないのですが、Instrumentで計測した結果、シュミレーターで10回中5回ほどメモリリークが起きています。
何か対処方法があればご教授いただきたいです。
###補足情報(言語/FW/ツール等のバージョンなど)
Xcode8 Swift3 RealmSwift2.0.2