Swift3 - Background Fetch Crash on Debug
As per your above description, I tried it in my project but it works fine and performFetchWithCompletionHandler
is working in background also.
First I try without adding this property, but performFetchWithCompletionHandler not called. So,Try to add this in info.plist file
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
It will solve my problem
App crashes at reloadData() for Collectionview with a background fetch
Don't initialize PublicationOverviewController()
again because it will give you new instance of that view and you would not get your referenced collectionView
. Also you will get all UI controls nil before viewDidLoad:
get calls.
Example:
let *controller : PublicationOverviewController = PublicationOverviewController()
// controller.collectionView will be nil here because view hasn't loaded yet
What you can do Post a notification in applicationDidBecomeActive:
method and observe
it in PublicationOverviewController
then reload your collectionView
in respective method.
Background performFetchWithCompletionHandler using Blocks causes crash
self.completionHandler = UIBackgroundFetchResultNewData;
does not match types. completionHandler
is of type (void (^)(UIBackgroundFetchResult))
while UIBackgroundFetchResultNewData
is of type NSUInteger
.
typedef enum : NSUInteger {
UIBackgroundFetchResultNewData,
UIBackgroundFetchResultNoData,
UIBackgroundFetchResultFailed
} UIBackgroundFetchResult;
So when you call self.completionHandler (YES)
, self.completionHandler
is an NSUInteger
, so NSUInteger(YES)
doesn't make much sense.
Correct use of background fetch completion handler
Update your getZoneChanges
to have a completion parameter.
func getZoneChanges(completion: @escaping (Bool) -> Void) {
// the rest of your code
operation.fetchRecordZoneChangesCompletionBlock = { (error) in
if let error = error {
print("Error fetching zone changes: \(error.localizedDescription)")
completion(false)
} else {
print("Changes fetched successfully!")
completion(true)
}
}
}
Then you can update the app delegate method to use it:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
DispatchQueue.global(qos: .userInitiated).async {
// Code to get a reference to main view controller
destinationViewController.getZoneChanges { (success) in
completionHandler(success ? .newData : .noData)
}
}
}
Related Topics
Info.Plist Utility Error: "Info.Plist Couldn't Be Opened Because There Is No Such File"
Uinavigationcontroller Interactivepopgesturerecognizer Working Abnormal in iOS7
iOS 10 Heading Arrow for Mkuserlocation Dot
Trouble Creating Xmpp Muc Room: Code 503 (Service Unavailable)
Delete a Device from My Developer Account to Increase the Count
Swiftui Transitions: Scale from Some Frame - Like iOS Homescreen Is Doing When Opening an App
Ios11 Causing Cors Issues in All Mobile Browsers
Setneedsdisplayinrect: Causes the Whole View to Be Updated
Domain=Nsurlerrordomain Code 1202
How to Import Existing Objective C Classes in Swift
iOS 7 Device Token Is Different for Same Device
Disable Autolayout Localization Behavior (Rtl - Right to Left Behavior )
Uitableviewcell Height Resize When Image Is Downloaded