簡體   English   中英

當設備在Objective-C和Swift中處於離線狀態時執行一種方法

[英]Conduct a method when a device is offline with Parse in Objective-C and Swift

我想知道當Parse通知設備離線時如何執行一種方法。 我知道Parse在Xcode的輸出部分中發布了一條錯誤消息,通知該設備處於脫機狀態。 但是,我不知道錯誤發布后如何執行一種方法。 我附上了我正在使用的代碼。 謝謝!

 NSData *imageData = UIImageJPEGRepresentation(self.image, 1.0);
    PFFile *parseImageFile = [PFFile fileWithName:@"uploaded_image.jpg" data:imageData];
    [parseImageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            if (succeeded) {
                //Putting the photo in Parse
                PFObject* posts = [PFObject objectWithClassName:@"Tops"];
                posts[@"imageText"] = clothesName;
                posts[@"uploader"] = [PFUser currentUser];
                posts[@"imageFile"] = parseImageFile;
                [posts saveInBackground];
                NSLog(@"success!!");

使用可達性可能會更好,因為它是處理連接問題的標准類。 我建議使用Tony Million的實現,該實現是直接替代(升級)的。 可以在這里找到: https : //github.com/tonymillion/Reachability

文檔中的簡短示例:

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
    // keep in mind this is called on a background thread
    // and if you are updating the UI it needs to happen
    // on the main thread, like this:

    dispatch_async(dispatch_get_main_queue(), ^{
      NSLog(@"REACHABLE!");
      //Do the things you need to do with Parse here
    });
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
    //Wuh woh. Fire off an alert letting the user know there's no connection.
};

// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];

想要使用可達性的另一個原因嗎? 這也是Parse建議的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM