简体   繁体   English

处理“互联网不可用”的最佳实践(Angular,Ionic,Cordova App)

[英]Best practice to handle 'Internet not Available' (Angular, Ionic, Cordova App)

I know that similar questions have been asked before. 我知道以前也曾问过类似的问题。 But, I do not find a satisfactory answer. 但是,我找不到令人满意的答案。 So, I am posting this. 所以,我发布这个。

I have an app, that pulls data from a server using REST API calls. 我有一个应用程序,可以使用REST API调用从服务器提取数据。 I have 4 primary list views / tabs. 我有4个主要列表视图/标签。 Now here are a few scenarios: 现在,这是一些方案:

  1. Internet is available - All is good. 互联网可用-一切都很好。 You navigate back and forth between the views and you fetch the any new data on the server. 您在视图之间来回导航,然后获取服务器上的所有新数据。 Previously fetched data is part of the cache view that Ionic provides. 先前获取的数据是Ionic提供的缓存视图的一部分。 All good. 都好。

  2. Internet is disconnected. 互联网已断开连接。 Immediately user navigates to another view. 用户立即导航到另一个视图。 I am not able to use the Offline / Online event that Cordova / Angular provides as it takes a while after the disconnection for the event to fire. 我无法使用Cordova / Angular提供的“脱机/联机”事件,因为断开连接后需要一段时间才能触发该事件。 Meanwhile, the screen navigation has fired some $http calls (on view Enter event) to my server and returns an error because internet is not available. 同时,屏幕导航已向我的服务器触发了一些$ http调用(在View Enter事件上),并由于互联网不可用而返回错误。 I dont think it is wise to generalize all $http errors as Internet not available (is there a way to find that error is because of NO INTERNET?). 我认为将所有$ http错误都归纳为Internet不可用是不明智的(是否有方法可以找到该错误是由于没有Internet?)。 It could be that the error is because of some other error on server side. 该错误可能是由于服务器端的其他错误引起的。 I want to be able to navigate to a generic error page if this happens. 如果发生这种情况,我希望能够导航到一般错误页面。

  3. Internet is disconnected, user tries to navigate to a new view. Internet已断开连接,用户尝试导航到新视图。 If this new view is loaded with data then just show the data that was cached previously when internet was available. 如果此新视图中加载了数据,则仅显示Internet可用时先前缓存的数据。 If no data was loaded, then show a popup saying "Please fix your internet". 如果未加载任何数据,则显示一个弹出窗口,提示“请修复您的互联网”。 On click of Ok, navigate back to the view where the user came from. 单击“确定”后,导航回用户来自的视图。 I haven't found any good solution to achieve this. 我尚未找到实现此目标的任何好的解决方案。 Ofcourse, I can handle this in the Controller of every view, but I would like to make a generic service or use an httpinterceptor who can do all this for me once for all the views. 当然,我可以在每个视图的Controller中进行处理,但是我想提供一个通用服务或使用httpinterceptor,它可以为我为所有视图一次完成所有这些操作。

Any robust solutions on how all the above scenarios can be handled gracefully? 关于如何妥善处理上述所有方案的任何强大解决方案?

for 2: 2:

for any http call, if failed, call 'AnotherApi' which is a 'Very simple Api that is very unlikely to fail on the Server side', if this 'AnotherApi' fails, it's a no internet problem, otherwise you can show your generic Server error page. 对于任何http调用,如果失败,则调用“ AnotherApi”,这是“非常简单的Api,不太可能在服务器端失败”,如果此“ AnotherApi”失败,则说明没有互联网问题,否则您可以显示通用服务器错误页面。

to make sure this 'AnotherApi' works, you can place it in Another Domain/Another physical location or simply call any Services available in the internet. 为确保此“ AnotherApi”有效,您可以将其放置在“另一个域/另一个”物理位置中,也可以简单地调用Internet上可用的任何服务。

for 3: 3:

in my case, i do not retrieve data from Api directly, in all my views, retrieve data and display data are handled separately. 就我而言,我不会直接从Api检索数据,在我所有的视图中,检索数据和显示数据都是分开处理的。 after retrieve data from webservices and/or signalr, I put the data in internal arrays or internal storage or cookie or client side sqlite. 从Web服务和/或Signalr检索数据后,我将数据放入内部数组或内部存储或cookie或客户端sqlite中。 To display data, I always retrieve data from client side storage. 为了显示数据,我总是从客户端存储中检索数据。

I have a signalr connection (something like socket connection) to make sure the display data is the possible newest data since last success internet connection available, the signalr Server tells me if there is any new data in the Server. 我有一个信号器连接(类似于套接字连接),以确保显示数据是自上次成功的Internet连接以来可能的最新数据,信号器服务器会告诉我服务器中是否有任何新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM