I use my app since a 10 years, and recently I realised that the app consumes a lot of data (in gigabytes within a few days) and it actually does nothing... only sync a few simple records with iCloud database. So I have tried to debug where the app consumes the data, and I simplified everything.
Now my app is just simple brown controller:
import Alamofire
import CoreData
import Foundation
import UIKit
import SwiftSoup
import StoreKit
import WebKit
import Firebase
import SVProgressHUD
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow()
window.rootViewController = UIViewController()
window.rootViewController?.view.backgroundColor = .brown
window.backgroundColor = .white
self.window = window
self.window?.makeKeyAndVisible()
return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
print("active")
}
}
And that is all. I commented out everything for debug time. And that is most simplified code as possible. And the app still consumes data in megabytes every time when application did become active (active is printed). Why? Where else I can search for the source of that issue?
How do I know it? I go to Settings > Mobile Data > My App
- and it increases ~3-10 MB every time when app did become active. Why?
Update
After a big research and long debugging I have discovered that for high data consumption are responsible widgets of my app. I have 5 widgets, and each of them display images from URL the following way:
extension Image {
init(url: URL?) {
if let url = url, let imageData = try? Data(contentsOf: url), let uiImage = UIImage(data: imageData) {
self.init(uiImage: uiImage)
} else {
self.init(systemName: "photo.fill")
}
}
}
It seem that each time when an app did become active every widget is refreshed. And in total there is 20-30 images displayed from URL.
When I remove the widgets from home screen, or remove widgets from embedded content the issue does not exist any more.
Is there a way to cache it or improve performance?