| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
wdwu
9年前发布

Xamarin: 为你的iOS应用添加后台数据刷新

来自: https://blog.xamarin.com/add-background-data-refresh-to-ios-apps/

Users expect your apps to load quickly. A critical component of a user’s perception of your app is how fast it loads data. There are many steps you can take to speed up network requests, including general networking techniques like request prioritization and speculative requests or taking advantage of the native networking stack on iOS or Android withModernHttpClient. You can also utilize caching so that users can at least view a partial data set while they wait for new data from the server.

But what if your data was there before the user even opened your app? This may seem extremely difficult or even impossible, but many apps you use every day such as 非死book and 推ter are already utilizing this powerful feature. In this blog post, we’re going to walk through the process of adding background data refresh in just three simple steps to your iOS apps.

  1. Edit your Info.plist to enable Background fetch .
  2. Set the minimum background fetch interval.
  3. Override the PerformFetch method in the AppDelegate .

Request Permission for Background Fetch in Info.plist

Open the Info.plist file within either Xamarin Studio or Visual Studio and scroll to the bottom. Select the Background fetch property, and make sure that Enable Background Modes is checked.

Adding Background Refresh

To minimize the data and battery power consumed during background refresh, iOS requires that you refresh no more often than every 15 minutes, though it’s unlikely you’ll need to refresh your data more than this. To set the minimum background refresh level, call the application delegate’s SetMinimumBackgroundFetchInterval method and pass in a double, representing the minimum number of seconds between a background refresh:

// Minimum number of seconds between a background refresh  // 15 minutes = 15 * 60 = 900 seconds  private const double MINIMUM_BACKGROUND_FETCH_INTERVAL = 900;     private void SetMinimumBackgroundFetchInterval ()  {      UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval (MINIMUM_BACKGROUND_FETCH_INTERVAL);  }

Performing Background Fetch

Next, override the PerformFetch method in your AppDelegate . This is the code that will be executed when your app is ready to refresh data. Be sure to call the completion handler and pass in the download status; if this is not called, your application will be terminated by the OS:

// Called whenever your app performs a background fetch  public override void PerformFetch(UIApplicationapplication, System.ActioncompletionHandler)  {      // Do Background Fetch      var downloadSuccessful = false;      try       {          // Download data          await Client.Instance.BeerDrinkinClient.RefreshAll();          downloadSuccessful = true;      }       catch (Exceptionex)       {          Insights.Report(ex);      }         if (downloadSuccessful)       {          completionHandler (UIBackgroundFetchResult.NewData);      }       else       {          completionHandler (UIBackgroundFetchResult.Failed);      }  }

Conclusion

Background data refresh is an important part of ensuring that your app always has the latest data ready to present to the user and will vastly increase the speed perception of your application. iOS makes it very easy to implement background refresh in just three easy steps and less than 75 lines of code. To see background data refresh in action with a real app, check out our BeerDrinkin’ sample.

 本文由用户 wdwu 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1456886809546.html
Xamarin iOS开发 移动开发