- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在设计一个 iOS 5 iPhone 应用程序,它将使用 .NET RESTful Web 服务来提供数据更新。当应用程序最初安装时,它将连接到WS以下载JSON格式的所有数据。此后,它将仅执行更新。 WS 为每个表提供了一个 POST 方法,即 GetAllTableRecords() 和 GetLastUpdatedTableRecords()。
我使用的是 iOS 5,并且 NSURLConnection 和 JSON 序列化/反序列化可以与 native 库正常工作。每个 WS POST 方法调用当前都驻留在其自己的具有所有委托(delegate)方法的 Obj-C 类中。此外,每个类都处理本地数据存储插入和更新。
每个 NSURLConnection 都是异步的,所有 WS 调用都是由 View Controller 的按钮事件驱动的。
我的问题是:
目前有两个表格可供下载。这意味着应用程序将调用 WS 两次以获取初始数据,并在每次刷新期间再次调用两次。我知道,由于每个 NSURLConnection 都是异步的,因此连接将发出请求,但 UI 将在委托(delegate)处理数据下载时继续运行。我对 GCD 和 NSOperation/Queue 进行了一些研究,但我对其中任何一个都没有足够的了解来编写解决方案,也不知道这是否是正确的解决方案。
任何见解都会非常有帮助!
编辑 #1:向 UI 提供实时更新怎么样? Mint 应用程序在更新交易和帐户时会执行类似的操作。它们有一个小状态栏,在发出请求时会在底部弹出。
编辑#2:好的,我相信我已经取得了一些进展。我们正在使用 Storyboard,入口点是登录 View / Controller 。单击登录按钮时,将与 Web 服务建立 NSURLConnection。如果connectionDidFinishLoading:(NSURLConnection *)connection中的响应状态代码为200,则执行segue以进入数据同步 View 。此 View 的目的是初始化或更新数据库,同时向用户提供反馈。更新或初始化都需要两次额外的 Web 服务调用。
这是我的DataSyncView.m:
@synthesize pvStatus, lbStatus;
// pvStatus = progress indicator
// lbStatus = label
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self StartDataSync];
}
- (void)StartDataSync
{
[lbStatus setText:@"Syncing data..."];
[pvStatus setProgress:0.0f];
// TODO: Determine if database is popuplated
[self PerformInitialSync];
// Next screen
[self performSegueWithIdentifier:@"SegueFromSync" sender:self];
}
// Populates data store will data from web service
- (void)PerformInitialSync
{
// Kicks off a series of synchronous requests
[self DownloadAllEmployeeDataA];
}
- (void)DownloadAllDataA
{
// Dictonary holds POST values
NSMutableDictionary *reqDic = [NSMutableDictionary dictionary];
// Populate POST key/value pairs
[reqDic setObject:passWord forKey:@"Password"];
[reqDic setObject:userName forKey:@"UserName"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:reqDic options:NSJSONWritingPrettyPrinted error:&error];
// Convert dictionary to JSON
NSString *requestJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// Declare Webservice URL, request, and return data
NSURL *url = [[NSURL alloc] initWithString:@"http://wsurl/getalla"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *postData = [NSData dataWithBytes:[requestJSON UTF8String] length:[requestJSON length]];
// Build the request
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setTimeoutInterval:60.0];
NSURLResponse *response;
[lbStatus setText:@"Downloading employee data..."];
[pvStatus setProgress:0.1f];
// Make the response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// If return data received
if(returnData)
{
// Get the response and check the code
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];
// Check to make sure successful code
if (code == 200)
{
// Convert JSON objects to Core Data Entity
// Update UIProgressView and Label
// Call next WS call
[self DownloadAllEmployeeDataA];
}
}
}
- (void)DownloadAllDataB
{
// Same code as above but with different URL and entity
}
我遇到的问题是:UIProgressView 和 Label 在进行调用时没有更新。正如我之前所说,我什至不知道这是否是调用这些电话的最佳方式。看来我没有阻塞主线程,但我可能是错的。我将再次提出问题:在保持 UI 进度更新的同时进行多个 url 调用的最佳方法是什么?谢谢!!!
最佳答案
// Make the response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
在你的问题中,你说你异步加载了 url 请求。但是在上面的代码行中您正在发出同步请求?
就代码封装和重用而言,这是正确的设置吗?
如何处理多个 WS 调用,同时通过 UI 通知用户?
我遇到的问题是:UIProgressView 和 Label 在进行调用时没有更新。
引用URL Loading System Programming Guide
我的另一个评论是你的方法名称,方法名称以小写字母开头。其余部分看起来不错。 Coding Guidelines for Cocoa
关于iphone - iOS 5 NSURLConnection - 通过 UI 反馈建立多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9561318/
我正在尝试使用 Excel 中的间接函数来构建公式以在另一张纸上返回值。 在工作表 A 单元格 D3 的值为 B 我想使用值 B 从名为 App Summary 的工作表中的单元格 B6 返回一个值。
我目前正在使用 LumiSoft 的 SIP 堆栈,并且能够在我的 FreePBX 盒子上成功注册分机并调用另一个软电话。我现在需要做的就是通过调用流式传输 WAV 文件(或 RAW,或任何可行的文件
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (65 个回答) 已关闭 8 年前。 我有一个安卓 Activ
我正在使用 ws npm 在服务器端,websocket 在客户端。 从 node-js 运行此代码时它工作正常,但从浏览器运行它会出现以下错误: failed: Error in connectio
当我将鼠标悬停在想要淡入和淡出的内容上多次时,它就会不断重复。即使我停止悬停它。我怎样才能阻止这个? $(".featured").hover(function(){ $(this).find
我需要建立一个 mysql 连接并取回一些数据。我可以使用此代码在 Java 中执行此操作 try{ String username;
不能制造愚蠢。具有下一个文件夹结构: /flint/double-conversion/src /燧石/愚蠢/愚蠢/ 其中/flint/folly 包含自述文件和许可证。作为in the readme
我想在编译主单元之前在程序集中嵌入本地引用。但书面目标不起作用。 WithMetadataValue( 'CopyLocal', 'true' )->Met
我不是软件专家,但我确实需要一些建议。 我正在编写一个 C 程序(在下面剪切/粘贴)以通过 LAN(以太网)建立从我的 Mac Pro 到位于它旁边的基于 Windows XP 的测试仪器的 TCP
我正在构建一个应用程序,我的手机经常将数据发送到我的服务器。由于我将使用我的移动数据,我想知道建立(和拆除?)到我的服务器的 TCP 连接需要多少数据。 最佳答案 TCP 三向握手 Device 1
我有一个带有登录表单的网站。当加载登录表单页面时,我创建一个新的 PDO 对象以查看连接是否正常工作。如果成功打开连接,查看者将看到一个登录表单。如果不成功,他们会收到一条消息,说明服务器已关闭。 然
构建我的Electron应用程序后,它将显示产品名称undefined。如何设置其他名称呢? 当前是这样的: 最佳答案 请尝试此操作。引用此链接 https://www.electronjs.org/
我有一个项目在哪里使用这个 jar 。 据我所知...发生 war 之后,文件夹WEB-INF/lib必须具有: mail-1.4.1.jar activation-1.1.jar mysql-con
代码: %{ #include #include #include #include "gener.h" #include "sym_tab.h" #include "scope.h" #inc
我需要将侧边栏小部件集成到我的高流量页面(称为SiteA)中。该小部件应包含我的其他页面之一(称为 SiteB)的最新文章。 在我看来,我有两种可能的解决方案。 SiteA 上的 cUrl 调用从 S
我正在尝试建立 Cortana 技能,以便能够使用 Surface 相机拍照。怎么做?目前我的技能是能够使用bot框架和使用nodejs来回答问题。代码看起来像 bot.dialog('ScanCar
这个问题在这里已经有了答案: Resolving javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExce
当我与网络服务器建立 https 连接时出现 SSLProtocolException。我只在 Android 2.3 Gingebread 中有这个异常(exception);相同的代码在所有其他
我想做的是指定几个端口,然后检查它们是否已建立连接。我找到了以下脚本并运行了,但它只列出了 3 个端口,我不明白为什么。我验证了相关端口的事件规则(以及下面输出中未列出的许多其他端口)。 Set ob
使用 MySQL 我试图使用已经上传到数据库中的数据建立一对多关系。举个例子,假设我在一个表中有一个名字列表,我想将它们连接到一个他们去过的地方的列表。显然 1 个人可以去很多不同的地方,但我在设置时
我是一名优秀的程序员,十分优秀!