iOS/Sample : UI need to be updated on Main Thread (#740)
Summary: ### iOSSample: Fix iOS Sample App crash: UI need to be updated on Main Thread ### SampleSwift: * Upgrade swift version * Fix force unwrapping * UI need to be updated on Main Thread ## Changelog Pull Request resolved: https://github.com/facebook/flipper/pull/740 Differential Revision: D19397414 Pulled By: passy fbshipit-source-id: 7af84c0fe43861aff6a18c36bf81a940baec5147
This commit is contained in:
committed by
Facebook Github Bot
parent
4530eb9235
commit
f6831e837a
@@ -44,15 +44,13 @@
|
|||||||
[[[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable dataTaskError) {
|
[[[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable dataTaskError) {
|
||||||
|
|
||||||
if (dataTaskError || !data) {
|
if (dataTaskError || !data) {
|
||||||
UIAlertController *alertController = [weakSelf alertControllerForMessage:@"Received error in POST API response"];
|
[weakSelf showAlertWithMessage:@"Received error in POST API response"];
|
||||||
[weakSelf presentViewController:alertController animated:true completion:nil];
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&dataTaskError];
|
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&dataTaskError];
|
||||||
NSLog(@"MSG-POST: %@", dict[@"msg"]);
|
NSLog(@"MSG-POST: %@", dict[@"msg"]);
|
||||||
|
|
||||||
UIAlertController *alertController = [weakSelf alertControllerForMessage:@"Received response from POST API"];
|
[weakSelf showAlertWithMessage:@"Received response from POST API"];
|
||||||
[weakSelf presentViewController:alertController animated:true completion:nil];
|
|
||||||
|
|
||||||
}] resume];
|
}] resume];
|
||||||
}
|
}
|
||||||
@@ -61,17 +59,21 @@
|
|||||||
__weak NetworkViewController *weakSelf = self;
|
__weak NetworkViewController *weakSelf = self;
|
||||||
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"https://demo9512366.mockable.io/FlipperGet"] completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
|
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"https://demo9512366.mockable.io/FlipperGet"] completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
|
||||||
if (error || !data) {
|
if (error || !data) {
|
||||||
UIAlertController *alertController = [weakSelf alertControllerForMessage:@"Received error in GET API response"];
|
[weakSelf showAlertWithMessage:@"Received error in GET API response"];
|
||||||
[weakSelf presentViewController:alertController animated:true completion:nil];
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
||||||
NSLog(@"MSG-GET: %@", dict[@"msg"]);
|
NSLog(@"MSG-GET: %@", dict[@"msg"]);
|
||||||
UIAlertController *alertController = [weakSelf alertControllerForMessage:@"Received response from GET API"];
|
[weakSelf showAlertWithMessage:@"Received response from GET API"];
|
||||||
[weakSelf presentViewController:alertController animated:true completion:nil];
|
|
||||||
}] resume];
|
}] resume];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)showAlertWithMessage:(nonnull NSString *)msg {
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
UIAlertController *alertController = [self alertControllerForMessage:msg];
|
||||||
|
[self presentViewController:alertController animated:true completion:nil];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
- (UIAlertController *)alertControllerForMessage:(nonnull NSString *)msg {
|
- (UIAlertController *)alertControllerForMessage:(nonnull NSString *)msg {
|
||||||
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Flipper" message:msg preferredStyle:UIAlertControllerStyleAlert];
|
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Flipper" message:msg preferredStyle:UIAlertControllerStyleAlert];
|
||||||
|
|||||||
@@ -373,7 +373,7 @@
|
|||||||
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.flipper.SampleSwift;
|
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.flipper.SampleSwift;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||||
SWIFT_VERSION = 4.0;
|
SWIFT_VERSION = 5.0;
|
||||||
TARGETED_DEVICE_FAMILY = "1,2";
|
TARGETED_DEVICE_FAMILY = "1,2";
|
||||||
};
|
};
|
||||||
name = Debug;
|
name = Debug;
|
||||||
@@ -424,7 +424,7 @@
|
|||||||
OTHER_SWIFT_FLAGS = "$(inherited) -Xcc -fmodule-map-file=\"${PODS_ROOT}/Headers/Public/FlipperKit/FlipperKit.modulemap\" -Xcc -DFB_SONARKIT_ENABLED";
|
OTHER_SWIFT_FLAGS = "$(inherited) -Xcc -fmodule-map-file=\"${PODS_ROOT}/Headers/Public/FlipperKit/FlipperKit.modulemap\" -Xcc -DFB_SONARKIT_ENABLED";
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.flipper.SampleSwift;
|
PRODUCT_BUNDLE_IDENTIFIER = com.facebook.flipper.SampleSwift;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
SWIFT_VERSION = 4.0;
|
SWIFT_VERSION = 5.0;
|
||||||
TARGETED_DEVICE_FAMILY = "1,2";
|
TARGETED_DEVICE_FAMILY = "1,2";
|
||||||
};
|
};
|
||||||
name = Release;
|
name = Release;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||||||
var window: UIWindow?
|
var window: UIWindow?
|
||||||
|
|
||||||
|
|
||||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||||
window = UIWindow()
|
window = UIWindow()
|
||||||
|
|
||||||
let client = FlipperClient.shared()
|
let client = FlipperClient.shared()
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ class NetworkViewController: UIViewController {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let dict = try! JSONSerialization.jsonObject(with: dataUnwrapped, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [String: String]
|
let dict = try? JSONSerialization.jsonObject(with: dataUnwrapped, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as? [String: String]
|
||||||
// As sonar cannot detect print() in Logs
|
// As sonar cannot detect print() in Logs
|
||||||
NSLog("MSG-GET: \(dict["msg"] ?? "Did not find msg key in the received response")")
|
NSLog("MSG-GET: \(dict?["msg"] ?? "Did not find msg key in the received response")")
|
||||||
strongSelf.showAlert(message: "Received response from GET API, please check the sonar network plugin for detailed response")
|
strongSelf.showAlert(message: "Received response from GET API, please check the sonar network plugin for detailed response")
|
||||||
}
|
}
|
||||||
dataTask.resume()
|
dataTask.resume()
|
||||||
@@ -55,9 +55,9 @@ class NetworkViewController: UIViewController {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let dict = try! JSONSerialization.jsonObject(with: dataUnwrapped, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [String: String]
|
let dict = try? JSONSerialization.jsonObject(with: dataUnwrapped, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as? [String: String]
|
||||||
// As sonar cannot detect print() in Logs
|
// As sonar cannot detect print() in Logs
|
||||||
NSLog("MSG-POST: \(dict["msg"] ?? "Did not find msg key in the received response")")
|
NSLog("MSG-POST: \(dict?["msg"] ?? "Did not find msg key in the received response")")
|
||||||
strongSelf.showAlert(message: "Received response from POST API, please check the sonar network plugin for detailed response")
|
strongSelf.showAlert(message: "Received response from POST API, please check the sonar network plugin for detailed response")
|
||||||
}
|
}
|
||||||
dataTask.resume()
|
dataTask.resume()
|
||||||
@@ -85,9 +85,11 @@ class NetworkViewController: UIViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func showAlert(message: String) {
|
func showAlert(message: String) {
|
||||||
|
DispatchQueue.main.async {
|
||||||
let alertController = UIAlertController.init(title: "Flipper", message: message, preferredStyle: .alert);
|
let alertController = UIAlertController.init(title: "Flipper", message: message, preferredStyle: .alert);
|
||||||
let alertAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)
|
let alertAction = UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil)
|
||||||
alertController.addAction(alertAction)
|
alertController.addAction(alertAction)
|
||||||
present(alertController, animated: true, completion: nil)
|
self.present(alertController, animated: true, completion: nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user