ios - 实现从express.js服务器到Objective-C移动应用程序的自动加载数据

标签 ios node.js objective-c xcode

我有一个用 Objective-C 编写的应用程序,需要从 Node.js 服务器自动加载一些数据。

应用程序 1 向我的服务器发送一条消息,然后应用程序 2 需要接收该消息。应用程序 2 需要自动加载此消息(无刷新按钮)。这些消息是通用数据,而不是带有 APN 的远程通知。

我目前使用以下内容:

- (void) checkForNewMessages {

   // call the method on a background thread
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       [self->dataParser getMessages:^(NSArray *arr, NSError *error) {
           // If there's an error
           if (error) {
               return;
           }
           // otherwise
           [self->messageArray removeAllObjects];
           self-> messageArray = [NSMutableArray arrayWithArray:arr];
           if (self-> messageArray) { // is not nil
               // update UI on the main thread
               dispatch_async(dispatch_get_main_queue(), ^{
                   [self->newMessageTimer invalidate];
                   [self tableRefresh];
               });
           }
       }];
   });
}

它使用 GET 请求来拉取消息(由重复的 10 秒计时器控制,该计时器在找到消息时失效)。

我认为这会在后台运行时起作用 - 但这已经变得非常错误,并且依赖于计时器 - 它也经常在请求发生时卡住 UI,即使它应该在后台运行.

本质上,对于这种类型的功能是否有更好的解决方案 - 或者这对于生产应用程序来说似乎完全有效。

最佳答案

使用DISPATCH_QUEUE_PRIORITY_DEFAULT,您允许系统选择要使用的队列,有时它可以决定选择主队列,因为它足够自由。因此,在您的情况下,明确指定您需要后台队列是更好的方法,如下所示

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
       [self->dataParser getMessages:^(NSArray *arr, NSError *error) {
       ...

关于ios - 实现从express.js服务器到Objective-C移动应用程序的自动加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59938113/

相关文章:

ios - Xcode 12 模块 'RxSwift' 未在库演化支持下编译;使用它意味着无法保证 '' 的二进制兼容性

android - Delphi Seattle 无法引用默认的 FMX 类

无法使用javascript数组函数

Node .js : rendering error with vash

ios - 如何使用 UINavigationBar 中的 UITextView 导航到下一个 UIViewController

ios - 使用 WatchKit 2 的 iOS 应用程序中的 WCSession 停止应用程序构建

ios - 从过滤数据崩溃的 TableView 中删除项目

ios - 从 plist 中获取和比较 CFBundleVersion

node.js - 如何使 mongoDB 实例可用于 Node.js 中的所有其他模块

objective-c - 尝试从 UITableView 中删除单元格时出错