Swift leaks when using generic closure function with Double - ios

I have a simple array that holds generic functions. If store some listeners in this array I have a strange problem with leaks. I can use it without any problem with generic types of any kind but Optional Numeric types. If I use Double? for example I have a memory leak. Also problem only occurs when I capture unowned/weak self. Can someone explain me why this leak appear and how to fix this?
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var observations = [(Double?) -> Void]()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
observations.append { [unowned self] (new) in
print(self)
}
return true
}
}
If I change observations to [(String?) -> Void](), then there is no problem.

Related

SwiftUI: where to initialize frameworks?

With UIKit and UIApplicationDelegate logic, I used to initialize all the frameworks and stuff in the good old applicationDidFinishLaunching... like this:
#UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Parse.initialize(with: ParseClientConfiguration() {
$0.applicationId = PARSE_APP_ID
// ...
})
FirebaseApp.configure()
// etc...
}
}
I'm now learning how to use SwiftUI, but since there is no more UIApplicationDelegate, and I've never really used UISceneDelegate before, I don't really know where I should do this.
Thank you for your help.

Is there a purely SwiftUI way to call registerForRemoteNotifications()?

my app has a struct that implements App, and this struct uses a custom AppDelegate. This is my entire AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
application.registerForRemoteNotifications()
return true
}
}
I would like to get rid of this AppDelegate and call application.registerForRemoteNotifications() in the App implementation. Is this currently possible with SwiftUI 2.0 or later?

UIApplication delegate must be used from main thread only

I have a standard AppDelegate file in my Xcode project, I'm experimenting with Firebase and as soon as I imported it & added
FirebaseApp.configure()
I was flagged with purple warnings: Click me
The whole AppDelegate looks as follows
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}
These warnings never appeared whilst using Xcode 8, as a novice I am unsure on how to fix this, I have read that adding a DispatchQueue.main.async can fix it but I'm not sure where to add this.
Thanks!

Declaring variable outside the class vs inside

Hey I am trying to initialise a realm object in my app outside the app delegate.
Now, Since a lot of file in my app are accessing the realm file, i decide to declare it globally.
Now, by doing that I got an app rejection, reasoning crash due to this object. My code is as follows
AppDelegate.swift
import UIKit
import RealmSwift
var uiRealm = try! Realm()
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
In the above code the object is initialised as the app starts and any other methods loads.
Is it possible that I just declare an object or type Realm and the initialise it under didFinishLaunching as follows:
import UIKit
import RealmSwift
var uiRealm : Realm!
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// I initialise it here
uiRealm = try! Realm()
return true
}
Will it make a difference if its declare in such form. I am a little timid about this.
Any help is highly appreciated.
P.S: I already tried declaring the realm variable in app delegate and the calling it in other classes by UIapplication.shared.delegatemethod, and it always crashes.

Could not init a PFObject subclass

After upgrading to Xcode 6.3 6D570 (and Swift 1.2), the init of a subclassed object does not compile.
Let's say I have a class called Armor, which inherits from PFObject, PFSubclassing (exactly as Parse documentation says).
When I try to create an instance, like var armor = Armor(), I get the following compile error:
Missing argument for parameter 'className' in call
Then I read in Parse docs that I should use the 'object' class method to init a subclassed object. So I tried to init like this: var armor = Armor.object().
But then I get the following compile error:
'object()' is unavailable: use object construction 'PFObject()'
I'm using Parse SDK version 1.7.1.
I also override the parseClassName method as follows:
class func parseClassName() -> String {
return "Armor"
}
I registered the subclass inside the initialise method and on app delegate before I setup Parse:
override class func initialize() {
var onceToken : dispatch_once_t = 0;
dispatch_once(&onceToken) {
self.registerSubclass()
}
}
How should I properly init a subclassed object?
=========================================================================
THIS HAS BEEN FIXED IN 1.7.3 PARSE SDK
You can download the new version here:
https://parse.com/docs/downloads
=========================================================================
Though docs are not clear on that, using .object() in Swift is not required anymore.
as said here and in the Swift code snippet found here
Now, this is weird but to make Armor() work you need to reference PFUser class somehow in the same file. Maybe it doesn't have to be PFUser but I havent dug deeper into it.
So, this won't work
import UIKit
import Parse
import Bolts
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("appID", clientKey: "clientKey")
let myArmor = Armor()
return true
}
}
But this will
import UIKit
import Parse
import Bolts
private let fix = PFUser.hash() // here's the weird trick
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("appID", clientKey: "clientKey")
let myArmor = Armor()
return true
}
}
Hope this helps and Parse pushes a fix out soon.
Also reported as a bug
register the class as a subclass in the AppDelegate, but before Parse.initialize is called.
Then you can delete the overwritten function initialize
This would make:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]!) -> Bool {
Armor.registerSubclass()
// Further initialization
return true
}
then just initialize the class by calling the constructor:
var myArmor = Armor()

Resources