0

タイトルで説明されているように、このような NSURLConnection を実行しました。しかし、デリゲート メソッドを実行できないことがわかりました。誰でも対処方法を知っていますか?

編集: 私のデリゲートはメイン スレッドで動作しますが、NSURLConnection は操作キューで動作します。:NSURLConnection は正常に動作しますが、デリゲートは実行されません。

編集 2: クラス メソッド [NSURLConnection connectionWithRequest: delegate:] を使用しました

これが私のメインキューです

 NSOperationQueue *queuePhoto = [[NSOperationQueue alloc] init];
 NSInvocationOperation *invocationOperationPhotos = [[NSInvocationOperation alloc] initWithTarget:self   selector:@selector(transferToServerAddimages:) object:arrayOfASection]; 
//                        [invocationOperationPhotos addObserver:self forKeyPath:@"isExecuting" options:0 context:invocationOperationPhotos];
 //                        [invocationOperationPhotos addObserver:self forKeyPath:@"isCancelled" options:0 context:invocationOperationPhotos];
//                        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(<#selector#>) name:@"isExecuting" object:nil];
                        [invocationOperationPhotos setQueuePriority:NSOperationQueuePriorityHigh];
                    [queuePhoto addOperation:invocationOperationPhotos];
                    [mutableArrayPhotoQueue addObject:queuePhoto];
                    [invocationOperationPhotos release];
                    [queuePhoto release];

ここに私の NSURLConnnection があります:

- (void) transferToServerAddimages:(NSArray *) arrayToAdd
{
NSLog(@"[NSOperationQueue currentQueue]: %@", [NSOperationQueue currentQueue]);

NSString *murphoAppPrefix = [[AppDelegate sharedAppDelegate] murphoAppPrefix];
// setting up the request object now
NSString *urlString = [murphoAppPrefix stringByAppendingString:@"addPhotos.php"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

/*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type
 */
// set header value ,   some random text that will never occur in the body
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];

//  email part    
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n%@", self.trip.whoCreate.email] dataUsingEncoding:NSUTF8StringEncoding]];

//  password part
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n%@", self.trip.whoCreate.psw] dataUsingEncoding:NSUTF8StringEncoding]];

//  image part
NSInteger subCount=0;
for (NSDictionary *aDict in arrayToAdd) {
    //  belonging part

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"r_whichShortTrip%d\"\r\n\r\n%@", subCount, [aDict objectForKey:@"r_whichShortTrip"]] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uniqueId%d\"\r\n\r\n%@", subCount, [aDict objectForKey:@"uniqueId"]] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"createdTime%d\"\r\n\r\n%@", subCount, [aDict objectForKey:@"createdTime"]] dataUsingEncoding:NSUTF8StringEncoding]];

    UIImage *imageFile = [aDict objectForKey:@"image"];
    NSData *imageData = UIImageJPEGRepresentation(imageFile, 1.0);
    NSString *imageName = [[URLConnect createUUID] stringByAppendingString:@".jpg"];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat: 
                       @"Content-Disposition: form-data; name=\"image%d\"; filename=%@\r\n", subCount, imageName]                          dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    UIImage *thumbnail = [aDict objectForKey:@"thumbnail"];
    NSData *thumbnailData = [NSData dataWithData:UIImageJPEGRepresentation(thumbnail, 1)];
    NSString *thumbnailName = [[URLConnect createUUID] stringByAppendingString:@".jpg"];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat: 
                       @"Content-Disposition: form-data; name=\"thumbnail%d\"; filename=%@\r\n", subCount++, thumbnailName]                          dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:thumbnailData]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}


[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
[NSURLConnection connectionWithRequest:request delegate:self];

}

これが私の代理人です:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [[[AppDelegate sharedAppDelegate] mArrayOfFailedConnection] addObject:connection];
     [connection cancel];
     connection = nil;
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"response: %@", response);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connection: %@", connection);
}
4

2 に答える 2

0

実際には、操作のセレクターを「transferToServerAddimages」として設定すると、この関数が完全に実行されたときに操作が終了することを意味します。これは、NSUrlConnection が開始されたのと同じスレッドでデリゲートを呼び出すため、そのスレッド (OperationQueue) である可能性が高いためです。応答が到着する前に終了しました。実際に NSOperationQueue を使用する場合は、NSUrlConnection の「sendAsynchronousRequest:queue:completionHandler:」を使用することをお勧めします。

于 2012-10-23T07:58:29.453 に答える
0

デリゲートとはNSOperation何ですか? 1 つの可能性は、操作が呼び出し-[connectionWithRequest:delegate:]て終了し、終了して削除される可能性があります。NSLogオペレーションの dealloc に を入れて、実行中かどうかを確認します。

すべての操作がトランザクションであると仮定すると、これらのトランザクションは既に非同期であるため、同時実行性を得るためにNSURLConnectionトランザクションをラップする必要はありません。NSOperationただし、本当に使用したい場合NSOperation(調整目的など) は、「並行」してメイン キューに入れます。それらの種類はよりトリッキーですが、周りに例があります。

于 2013-06-22T22:51:14.147 に答える