我已经将NSOperation子类化了 - (void)main是正确的,(因为它在使用NSOperation之前工作),我还逐步完成了init方法,并且变量被正确初始化.在( - id)initWithSampleBuffer:完成之后所有的艰难,我正在尝试将uploadOperation添加到NSOperationQueue:
UploadOperation *ulOp = [[UploadOperation alloc] initWithSampleBuffer:sampleBuffer];
[queue addOperation:ulOp]; //here i get exc_bad_access
[ulOp release];
Run Code Online (Sandbox Code Playgroud)
我得到了exc_bad_access.我已经尝试过断点,我可以看到队列存在,ulOp也是如此.我无法弄清楚我做错了什么,因为根据我的理解,当你试图将"消息"传递给已经解除分配的东西时,会发生exc_bad_access,显然,没有一个是.
- (id)initWithSampleBuffer:(CMSampleBufferRef) aSampleBuffer {
sampleBuffer = aSampleBuffer;
VideoStreamViewController *vc = [VideoStreamViewController shared];
ul = [[Uploader alloc] initWithURL:[NSURL alloc] filePath:@"" delegate:vc doneSelector:@selector(didFinishUpload:) errorSelector:@selector(uploadFailed:)];
return self;
}
Run Code Online (Sandbox Code Playgroud)
然而上传者的东西,不是问题(我删除它,仍然得到相同的结果).从我可以看到CMSampleBuffer对象没有问题,它被初始化!
队列初始化:
在.h:
NSOperationQueue *queue;
@property (nonatomic, retain) NSOperationQueue *queue;
Run Code Online (Sandbox Code Playgroud)
在.m:
@synthesize queue;
self.queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
Run Code Online (Sandbox Code Playgroud) 有人能解释异步NSURL请求与GCD和NSOperationQueues之间的关系吗?
我不确定何时使用它们.
现在,当我需要将数据提取/上传到服务器时,我一直在"逃避"异步NSURL请求.但有人建议我应该使用GCD.我的问题是我不知道现实生活中的例子GCD会更好.有没有人对我有任何常见用例?例如,如果我使用GCD存储10个异步NSURL GET请求的队列,这对我有什么好处?在大型中央调度队列或NSOperationQueue中有异步NSURL请求是否有意义?
谢谢!
nsurlconnection nsoperationqueue grand-central-dispatch ios afnetworking
我正在使用NSOperationQueue排队并调用多个地理编码位置查找.我想在所有异步运行的查找完成后调用完成方法.
-(void)geocodeAllItems {
NSOperationQueue *geoCodeQueue = [[NSOperationQueue alloc]init];
[geoCodeQueue setName:@"Geocode Queue"];
for (EventItem *item in [[EventItemStore sharedStore] allItems]) {
if (item.eventLocationCLLocation){
NSLog(@"-Location Saved already. Skipping-");
continue;
}
[geoCodeQueue addOperationWithBlock:^{
NSLog(@"-Geocode Item-");
CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[self geocodeItem:item withGeocoder:geocoder];
}];
}
[geoCodeQueue addOperationWithBlock:^{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
NSLog(@"-End Of Queue Reached!-");
}];
}];
}
- (void)geocodeItem:(EventItem *)item withGeocoder:(CLGeocoder *)thisGeocoder{
NSLog(@"-Called Geocode Item-");
[thisGeocoder geocodeAddressString:item.eventLocationGeoQuery completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Error: geocoding failed for item %@: %@", item, error); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AFNetworking下载文件时测量每个GET请求所花费的时间.我正在循环中重复下载文件.我遇到的问题是我测量总时间的方式,它给出了比实际更大的总时间.例如,对于50次下载,它提供72秒,但实际上它只花了大约5秒.我还怀疑5秒太低而不能下载50次(每个文件的下载大小为581 kb).
在这种情况下,如何有效地测量时间?我需要时间从请求被解雇到收到响应.
我下载文件的方法:
- (void) HTTPGetRequest
{
startTime = CACurrentMediaTime(); // Start measuring time
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:http://myServer];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"/download/Text581KB.txt"
parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
// Save downloaded file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Text581KB.txt"]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
double elapsedTime = (CACurrentMediaTime() - startTime); // Measuring time
totalTime += elapsedTime; // Measuring total …
Run Code Online (Sandbox Code Playgroud) 我发现有几篇帖子询问如何运行后台任务。这可以。我得到它。Apple有一个指南,仅适用于某些类型的应用程序。
我的用例如下:我只想在聊天应用程序位于前台时更新该应用程序的联系人列表。因此,当应用程序分别处于以下状态时,我可以启动/暂停/恢复:didBegan、didEnterBackground、didResumeFromBackground。
我如何使用 GCD 来实现这一目标?
换句话说,我如何以重复的方式安排异步任务并且仅每隔一段时间(例如每 0.5 秒)调用一次?有没有使用 NSOperationQueue 的好的实现?
编辑2:我想要执行的任务:
1:从 Web 服务 API 获取包含联系人信息的 JSON 数据对象(在线状态、设备、上次查看时间)
2:从 Web 服务 API 获取包含给用户的消息的 JSON 数据对象
编辑: NSOperation 文档将操作定义为只能用作“单次”的操作,因此创建递归操作可能不是解决此问题的最佳方法。
iphone objective-c nsoperationqueue grand-central-dispatch ios
在我的代码中,我想在队列中单独处理操作,并能够暂停和恢复队列中的操作操作.我该如何实现呢?
让我简单解释一下我的问题,我使用下面的代码来创建子类NSOperation的操作,并将此操作添加到NSOperationQueue.
@interface MyLengthyOperation: NSOperation
@end
@implementation MyLengthyOperation
- (void)main
{
// a lengthy operation
@autoreleasepool
{
for (int i = 0 ; i < 10000 ; i++)
{
// is this operation cancelled?
if (self.isCancelled)
break;
NSLog(@"%f", sqrt(i));
}
}
}
@end
Run Code Online (Sandbox Code Playgroud)
我创建了上面的操作,我正在调用它
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
myQueue.name = @"Download Queue";
myQueue.maxConcurrentOperationCount = 4;
[myQueue addOperation:operation];
Run Code Online (Sandbox Code Playgroud)
现在我想暂停并恢复我的操作.让我们假设循环在6786上,我按下暂停按钮,然后队列应暂停此操作,当我按下启动时,它应该从6786开始.
我想控制队列中的多个操作.我怎样才能做到这一点 ?
我看了[queue setSuspended:YES];
- 这将阻止队列添加新操作,但我想控制队列中的现有操作.
等待你的答案.
提前致谢 !