- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
Apple 似乎建议使用运行循环与外部配件进行数据通信。但是,除非我遗漏了什么,否则 runloop 不太适合某些类型的通信。
我们有一个 -experimental- 配件,我们需要向其发送任意数量的字节(最多,比如 1024),然后配件处理该数据(可变延迟,比如 1ms 到 1000ms 之间),后跟来自配件的可变长度响应(最多 1024 字节)。
我们想开发一个静态库(框架)来与配件进行通信。基本上,这个库将有一个函数,它将 NSArray 或 NSMutableArray 作为输入,并返回包含响应的 NSArray 或 NSMutableArray。
问题是推荐的runloops策略不太适合这类应用。在静态库函数中,在准备好要传输的数据并调度传输之后,我们要进入某种“等待”状态。但是,这种等待状态不能基于轮询方法(例如等待由接收路由设置的 -synchronized- 变量),因为接收例程永远不会执行(因为它们在同一个线程上) .
如果我们不使用 runloops,那么我们就不知道什么时候读取数据,因为我们不知道数据什么时候到达。
关于如何解决这个问题有什么想法或建议吗?有没有例子?
最佳答案
这不是 runLoop 或 ExternalAccessories 问题。这是一个日常的 OOP 问题。
最好的方法是创建一个可以写入 outputStream 并等待响应的 Communication 对象。使用@protocols 来做到这一点! (事件监听器驱动的过程)
试试这个:
首先,您必须将输入/输出流附加到 runLoop:
[[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session inputStream] open];
[[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[session outputStream] open];
成为他们的代表:
[[session outputStream] setDelegate:self];
[[session inputStream] setDelegate:self];
一旦你成为委托(delegate)人,你必须实现这个方法:
-(void)stream:handleEvent:{};
这是将数据写入流的命令:
/* data is a NSData containing data to transmit. */
[[session outputStream] write:(uint8_t *)[data bytes] maxLength:[data length]];
这是一个示例代码,(一旦您创建了 session ,而我们期望的答案是一个字节):
在 Comm.h 中:
/* Define your protocol */
@protocol CommDelegate <NSObject>
-(void)byteReceived: (char) byte;
@end
@interface Comm <NSObject> {
[...]
id<CommDelegate> delegate;
}
@end
@property (nonatomic, retain) id<CommDelegate> delegate;
在 Comm.m 中:
@implementation Comm
[...]
-(id)init {
[...]
delegate = nil;
[...]
}
-(void)write: (NSData *) data {
[[session outputStream] write:(uint8_t *)[data bytes] maxLength:[data length]];
}
-(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)_event {
switch (_event)
{
case NSStreamEventHasBytesAvailable:
/* This part will be executed every time your rx buffer contains at least 1 byte */
switch(state) {
uint8_t ch;
/* Read byte per byte */
[stream read:&ch maxLength:1];
/* now ch contains a byte from your MFI device
** and 'read' function decrease the length of the rx buffer by -1 */
/* Now you can notify this to the delegate
*/
if(self.delegate != nil)
[delegate byteReceived: ch];
}
break;
}
}
your_app_controller.h:
@interface MyApp : UIViewController <CommDelegate> {
Comm comm;
}
@end
your_app_controller.m:
@implementation MyApp
-(id)init {
[...]
comm = [[Comm alloc] init];
[comm setDelegate: self]; /* Now your thread is listening your communication. */
}
-(void)write {
byte out = 'X';
[comm write: [NSData dataWithBytes: &out length: 1]];
}
-(void)bytereceived:(char)reply {
if(reply == 'Y') {
[self write];
//[self performSelectorInBackground:@selector(write) withObject:nil]; IT'S BETTER!!!
}
}
@end
希望这对您有所帮助!
关于ios - 阻止与 ios 配件的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19768512/
我刚开始学习JSP技术,遇到了瓶颈。 如何从 JSP 声明 block ? 这不起作用: ... 服务器说没有“out”。 U: 我确实知道如何使用返回字符串的方法重写代码,但是有没有办法在 ?
在一个字段中,我想设置一个具有自定义过滤器的自定义分析器-着眼于词干-因此,“闪存卡”和“闪存卡”的词根相同,因此返回的结果相同 当我运行以下查询时,我的命中率很高,但是“闪存卡”和“闪存卡”各自返回
快速提问。 我有一个通过 PInvoke 使用 native DLL 的应用程序,这个 DLL 可能会调用 PostQuitMessage()。 如何避免? (因为我的应用程序不应该关闭) 我试过 A
一些给定的 HTML 文章,例如: Content 与一些基本的 Jquery 结合使用,例如: $(".some_
我正在构建一个灯箱相册。当第一个图像加载时,CSS 转换起作用。当加载后的每个图像都没有。任何想法为什么?加载第一张之后的照片,但没有过渡。 Image.prototype.load = functi
这个问题在这里已经有了答案: Disable recent tasks button on Android 5.0 (2 个答案) 关闭 2 年前。 我知道这个问题之前在这里被问过 Android
我是 Objective-C 的新手,我只是想弄清楚我是否可以使用 block 或选择器作为 UIAlertView 的 UIAlertViewDelegate 参数 - 哪个更合适? 我已经尝试了以
我是 Linux (UNIX) 套接字下套接字编程的新手。我在 Internet 上找到了以下代码,用于为每个连接生成一个线程的 tcp 服务器。但是它不起作用。accept() 函数立即返回,不等待
recv()库函数手册页提到: It returns the number of bytes received. It normally returns any data available, up
我有一个用于其他项目的共享 ts 库。在这个库中有被同一个库的其他资源使用的资源。该库的结构分为 components/*、interfaces/*、services/* 等目录。在每个目录的根目录中
我想在同一行中一个接一个地显示我的 ListView ,但 ListView 显示每个新行中的每个项目。我怎样才能防止换行显示。以便它显示为段落 ListView.builder( shr
我有一个包含数千行的表格。 import React from "react" import { useSelector } from "react-redux"; import { useEffec
假设我通常希望收到关于代码中不完整模式的警告,但有时我知道某个函数的模式不完整,我知道这很好。 是still true GHC 的警告粒度是每个模块的,并且没有办法更改有关特定功能或定义的警告? 最佳
我的网络应用程序发送浏览器通知,我知道如何检查通知的浏览器权限,以及如果未授予权限,如何请求权限。 但是,即使用户授予我的站点发送通知的权限,她可能仍然无法收到通知,因为它们 might be dis
我有 Xcode 3.2.1,并且喜欢使用它,但是当我编辑文本中带有超链接的文件时(例如,带有引用的注释:# see http://example.com)Xcode 将文本变成可点击的超链接。尝试编
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我有一个在 MY_Controller 中运行的 acl。如果权限被拒绝,那么此刻,我只是执行 redirect('denied') - 这是一个非常基本的 Controller ,它加载一个非常基本
我一直很好奇尝试从 Chrome 切换到 Firefox Quantum,但是对于 Web 开发遇到了一个我无法轻松解决的主要障碍——它正在缓存我的本地主机文件,因此当我尝试在本地主机加载各种 emb
这真的让我很兴奋!在任何时候,我都会参与多个项目。当我退出Xcode时,下次打开Xcode时,我前一天的所有项目都会自动一一打开。 经常我最终编辑错误的文件,AHHHHHHHHHHH!我可以阻止这种行
我的Wiki上有500个左右的Spambot和大约5个实际注册用户。我已经使用nuke删除了他们的页面,但是他们一直在重新发布。我已经使用reCaptcha控制了spambot的注册。现在,我只需要一
我是一名优秀的程序员,十分优秀!