这与我的问题几乎相同,只是代码非常不同:http://www.cocoabuilder.com/archive/message/cocoa/2009/3/24/233015
我想将一些处理卸载到NSOperation,传递一个文件名作为NSOperation加载和解析的引用.应用程序EXEC_BAD_ACCESS在进入时崩溃-(void)init.
以下是我启动操作的方式:
int n = [files count];
for (int i = 0; i < n; i++) {
NSString *filename = [files objectAtIndex:i];
FilterParseOperation *parser = [[FilterParseOperation alloc] initWithContentsOfFile:filename];
[filterParseQueue addOperation:parser];
[parser release], parser = nil;
}
Run Code Online (Sandbox Code Playgroud)
在剥离了我在NSOperation中的所有内容之后,我仍然以崩溃告终.以下代码崩溃:
#import "FilterParseOperation.h"
@implementation FilterParseOperation
- (id)initWithContentsOfFile:(NSString *)aFilename {
filename = aFilename;
return self;
}
- (void)dealloc {
[filename release], filename = nil;
[super dealloc];
}
- (void)main {
// do nothing!
}
@end
Run Code Online (Sandbox Code Playgroud)
这是崩溃的汇编程序输出(我不是很容易理解它的内容).这在__opLock中的addOperation之后直接发生
0x305ce610 …Run Code Online (Sandbox Code Playgroud) 我的问题很容易描述,但似乎很难解决.问题是加载图标,使用IconDownloader.mApple提供的官方示例提供的自定义类,如果我发布视图,则可以避免崩溃.
我已将IconDownloader该类添加到我的应用程序中,但很明显,只有tableview是root用户才能使用此方法.最大的问题是视图不是根视图.Fe:如果我开始滚动我的第二个视图(应用程序现在加载图标),并且没有时间完成下载,我回到root,应用程序崩溃,因为必须使用新图标更新的视图不再存在了.
一种可能的解决方案可能是OperationQueue在视图中实现一个,但是使用这种方法我在更改视图时停止队列并在我回来时重新启动它并且有N个队列的想法不会让我热情.
有人找到了解决这个问题的好方法吗?
因此,我正在尝试执行所有在后台线程上下载数据的REST调用,以便UI保持响应.
我有一个包含NSOperationQueue的viewcontroller.我创建了一个我的导入器类的实例,它是NSOperation的子类.在我的导入器的main()方法中,我正在设置ASIHTTPDataRequest.我创建了请求,然后是开始请求的时间.
问题:通过在请求上调用"startAsynchronous"来启动请求时遇到了问题.委托回调永远不会被调用.它就像请求启动,下载其数据,但从不调用委托回调方法.
我的解决方案:当我同步启动请求时,一切似乎都运行正常(即回调等).这是正确的解决方案吗?
为什么同步调用有效,而不是异步?在Apples"TopSongs"示例之后,我正在为我的导入器类建模.
iphone asynchronous nsoperation asihttprequest nsoperationqueue
所以我已经阅读了一些有关多线程和NSOperation的内容,并想知道如何使用它来改进我的应用程序.使用乐器我已经隔离了一些我的应用程序绝对可以使用速度改进的地方.我的问题是,这些类型的东西是否适合使用NSOperation的另一个线程?
绘制视图:我有一个相当复杂的视图,需要一点时间来绘制.当它被绘制时,我经历了一些滞后.
分配和播放音频:我正在AVAudioPlayer播放一些背景音乐.当我分配它时,又一些滞后.
计算:我也在进行一些计算并与大量整数进行一些比较.
我力求为我的应用程序提供最佳性能,那么你会做什么?
我有网络服务,我想用它来上传图像到服务器,由wsdl2objc生成的Web服务代理类,它使用NSOperation执行soap调用.假设在上传过程中我按下主页按钮,应用程序进入后台模式,那将是什么情况?上传过程会终止吗?或者过程无论如何都会完成.
在创建NSOperation并将其放入NSOperationQueue时,我从未看到调用main().只有start()被调用.我真的没有做任何奇特的事.作为一个简单的测试,我写了这个:
NSOperationQueue *testOperationQueue = [[NSOperationQueue alloc] init];
MyTestOperation *testOperation = [[MyTestOperation alloc] init];
[testOperationQueue addOperation:testOperation];
Run Code Online (Sandbox Code Playgroud)
在MyTestOperation.m中:
- (void)main
{
NSLog(@"testing if main is getting called");
}
- (void)start
{
NSLog(@"testing if start is getting called");
}
Run Code Online (Sandbox Code Playgroud)
MyTestOperation.h看起来像这样:
#import <Foundation/Foundation.h>
@interface MyTestOperation : NSOperation
@end
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗
[编辑注意:我实际上是指非并发,而不是并发(如前一个标题所述).
我创建了一个NSOperation目标是从20个URL下载一些图像(如20).所以在这里NSOperation我创建20 AFImageRequestOperation添加它们NSOperationQueue并调用-waitUntilAllOperationsAreFinished队列.
问题是,它不等待,它立即返回.这是代码
- (void)main {
NSArray *array = [I have the 20 links stored in this array];
self.queue = [[NSOperationQueue alloc]init];
self.queue.maxConcurrentOperationCount = 1;
for (int i = 0; i < array.count; i++) {
NSURL *url = [NSURL URLWithString:[array objectAtIndex:i]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFImageRequestOperation *op = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) {
return image;
} success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
// SUCCESS BLOCK (not relevant)
} …Run Code Online (Sandbox Code Playgroud) cocoa-touch objective-c nsoperation nsoperationqueue afnetworking
在我的项目中,我需要将数据发送到服务器,为此我使用以下代码来完成任务:
- (void)sendJSONToServer:(NSString *) jsonString
{
// Create a new NSOperationQueue instance.
operationQueue = [NSOperationQueue new];
//
// Create a new NSOperation object using the NSInvocationOperation subclass to run the operationQueueTask method
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(operationQueueTask:)
object:jsonString];
// Add the operation to the queue and let it to be executed.
[operationQueue addOperation:operation];
}//End of sendJSONToServer method
-(void) operationQueueTask:(NSString *) jsonString
{
//NSOperationQueue *remoteResultQueue = [[NSOperationQueue alloc] init];
dispatch_queue_t myQueue = dispatch_queue_create("SERVER_QUEUE",NULL);
dispatch_async(myQueue, ^{
// Performing long running …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有几个类,彼此交叉.我需要连接这些类并创建它们的属性,但是有些类对另一个类是不可见的,我想这里的主要问题是包含头文件.请问您的建议是什么问题?MyOperationQueue类
#import <Foundation/Foundation.h>
#import "ContentTableView.h"
//#import "CustomTableViewCell.h"
//#import "ObjectForTableCell.h"
@interface MyOperationQueue : NSOperation
@property(assign, nonatomic) BOOL isCancelled;
@property(strong, nonatomic) ContentTableView* tableView; //unknown type name
Run Code Online (Sandbox Code Playgroud)
class ObjectForTableCell
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MyOperationQueue.h"
@interface ObjectForTableCell : NSObject
@property (strong, nonatomic) MyOperationQueue* currentQueue;//unknown type name
Run Code Online (Sandbox Code Playgroud)
class ContentTableView - 这里我没有警告
#import <UIKit/UIKit.h>
#import "CustomTableViewCell.h"
#import "Protocol.h"
#import "MyOperationQueue.h"
#import "ObjectForTableCell.h"
#import "ImageViewController.h"
@interface ContentTableView : UITableViewController<CellDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>
Run Code Online (Sandbox Code Playgroud)
谢谢我提前寻求帮助.
SomeOperation went isFinished=YES without being started by the queue it is in
public class SomeOperation : AsyncOperation {
//MARK: Start
public override func start() {
isExecuting = true
guard !isCancelled else {
markAsCompleted() //isExecuting = false, isFinished = true
return
}
doSomethingAsynchronously { [weak self] in
self?.markAsCompleted() //isExecuting = false, isFinished = true
}
}
//MARK: Cancel
public override func cancel() {
super.cancel()
markAsCompleted() //isExecuting = false, isFinished = true …Run Code Online (Sandbox Code Playgroud) nsoperation ×10
ios ×5
iphone ×5
objective-c ×4
cocoa-touch ×2
afnetworking ×1
asynchronous ×1
background ×1
lazy-loading ×1
nsthread ×1
performance ×1
swift ×1
xcode ×1