문제

From https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}

I'm a relatively new iOS6 programmer. First off, I think that with ARC it should be just receivedData = [NSMutableData data] ?

Secondly, how should I declare the receivedData instance variable? I'm guessing @property (strong, nonatomic) NSMutableData *receivedData; in the header and @synthesize receivedData in the implementation.

However, I'm still trying to grok multithreading and ARC in iOS6. Should the property declaration be

@property (strong, nonatomic) NSMutableData *receivedData;

or just

@property (strong) NSMutableData *receivedData;

for the received data in the delegate of an asynchronous NSURLConnection?

도움이 되었습니까?

해결책

You should implement the remain methods of the NSURLConnectionDelegate. That's where you will get the data. If you want to use blocks, for instance, you can use this. Atomic guarantees that even if multiple threads are accessing the ivar and changing it, a value will be always be there. You get some boost if you have it as nonatomic. Your application logic should be responsible for data integrity and not how a setter/getter are synthesised. So in general, should be nonatomic.

First off, I think that with ARC it should be just receivedData = [NSMutableData data] ?

Yes, its enough.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top