I have the following code:
- (UIImage *) getPublisherLogo
{
//check the cache if the logo already exists
NSString * imageUrl = [NSString stringWithFormat:#"%#/%#&image_type=icon", self.baseUrl, self.imageUrl_];
ASIHTTPRequest * imageRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageUrl]];
[imageRequest setTimeOutSeconds:30.0];
[imageRequest setDownloadCache:[ASIDownloadCache sharedCache]];
[imageRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[imageRequest setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[imageRequest setCompletionBlock:^(void){
UIImage *img = [UIImage imageWithData:[imageRequest responseData] ];
if (img){
return img;
}
}];
[imageRequest setFailedBlock:^(void){
NSLog(#"Error in pulling image for publisher %#", [[imageRequest error] userInfo]);
}];
[imageRequest startAsynchronous];
}
}
The issue is that the return value/UIImage is returned at a block. How do I avoid this?
You're unable to return anything from the completion block because it's returned void.
You'll probably need to create a new method like setLogo:(UIImage *)image on the object that's expecting the image to be set, and call that method from within the completion block.
You can place your img pointer outside the block and declare it __BLOCK and use it as a closure. But you really need to be asking yourself what do you plan to do with img, bearing in mind the call is made asynchronously. I would imagine you should make another call in the block to another method and pass in the populated image as a parameter.
For getting an object from the ASIHttpRequest response, I use notifications.
For example, in the calling viewController
- (void)viewDidLoad {
// Subscribe notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onGetPhoto:) name:#"getPhotoNotification" object:nil];
}
- (void)viewDidUnload {
[super viewDidUnload];
// Unsubscribe from notifications
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"getPhotoNotification" object:nil];
}
- (void)onGetPhoto:(NSNotification *)notification {
...
}
in your completion block
[[NSNotificationCenter defaultCenter] postNotificationName:#"getPhotoNotification" object:self userInfo:userInfo];
With your photo in userInfo.
Related
I'm developing an app to help me understand OBJECTIVE-X/OSX.
The app simply connects to Facebook and sends a notification using NSUserNotification.
It is working fine, but now I want to add some UI to the mix.
To make the example simpler, I want to update a label (NSTextField) to show the status of the Facebook connection.
Connecting…
Connected
Failed
I have the following code in one File FacebookRequest.m
- (void) connectFacebook{
if(self.account == nil){
self.account = [[ACAccountStore alloc]init];
}
ACAccountType *facebookAccount = [self.account
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{
ACFacebookAppIdKey: #"MY_CODE",
ACFacebookPermissionsKey: #[#"email",
#"user_about_me",
#"user_likes",
#"manage_notifications",
#"user_activities"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.account requestAccessToAccountsWithType:facebookAccount
options:options
completion:^(BOOL success, NSError *error){
if(success){
NSArray *accounts = [self.account accountsWithAccountType:facebookAccount];
self.account = [accounts lastObject];
}
else{
NSLog(#"Erro %#", [error description]);
}
}];
}
and the following one in my AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self.statusFacebook setStringValue:#"Connecting…"];
FacebookRequest *request = [[FacebookRequest alloc]init];
[request connectFacebook];
}
What is the best way to update the UI after the request is complete and I have an account?
I'm having troubles since the request is asynchronous and I can't return any value inside the requestAccessToAccountsWithType block. Another point is that if I put some "ifs" to check if my account is nil after it, it will be executed before the block has finished executing, so the account would still be nil.
Thanks!
PS.: Sorry for the English if it is not clear enough.
You may use NSNotificationCenter for this purpose:
[self.account requestAccessToAccountsWithType:facebookAccount
options:options
completion:^(BOOL success, NSError *error){
if(success){
NSArray *accounts = [self.account accountsWithAccountType:facebookAccount];
self.account = [accounts lastObject];
// You post a notification that the UI should update here
[[NSNotificationCenter defaultCenter] postNotificationName:#"UpdateUI" object:nil];
}
else{
NSLog(#"Erro %#", [error description]);
}
}];
Then, you add your viewController that should update its UI as an observer of this notification:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateUI) name:#"UpdateUI" object:nil];
}
- (void)updateUI {
// Here you actually update your UI
}
p.s. if you are not using arc you also remove the observer in dealloc:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
I have a question.
I have the following code:
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{
[[ClassA sharedInstance] someSingletonMethod:params1];
[ClassB classBMethod:params2];
[self currentClassMethod:params3];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"kSNotificationName" object:nil];
}];
}];
[self.myOperationQueue addOperation:op];
Is it safe to call singleton methods in a block? Is it safe to call class methods in a block? Is it safe to call "self" methods?
I have a following situation. I'm sending a batch of requests to server:
AFHTTPClient *client=[[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client enqueueBatchOfHTTPRequestOperations:reqOps progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(#"finished: %i of %i requests", numberOfFinishedOperations, totalNumberOfOperations);
[[PTDictionaryUpdate sharedInstance] debugPrint:[NSString stringWithFormat:#"finished: %i of %i requests", numberOfFinishedOperations, totalNumberOfOperations]];
} completionBlock:^(NSArray *operations) {
NSLog(#"operations finished");
Here how I'm handling responses.
I'm creating operations to handle completed requests.
for (int i=0; i<[operations count]; i++)
{
AFJSONRequestOperation *operation=[operations objectAtIndex:i];
if ((operation.error==nil) && (operation.response.statusCode==200))
{
id JSON=operation.responseJSON;
int handleMethodIndex=-1;
for (int j=0; j<[urls count]; j++)
{
if ([operation.request.URL isEqual:[urls objectAtIndex:j]])
{
handleMethodIndex=j;
};
};
switch (handleMethodIndex) {
case 0:
{
//[self countryUpdate:JSON];
NSInvocationOperation *invOp=[[NSInvocationOperation alloc] initWithTarget:self selector:#selector(countryUpdate:) object:JSON];
[invOp setQueuePriority:NSOperationQueuePriorityLow];
[handleJSONOperations addObject:invOp];
break;
}
case 1:
{
//[self regionsUpdate:JSON];
NSInvocationOperation *invOp=[[NSInvocationOperation alloc] initWithTarget:self selector:#selector(regionsUpdate:) object:JSON];
[invOp setQueuePriority:NSOperationQueuePriorityLow];
[handleJSONOperations addObject:invOp];
break;
}
//.......
//.......
}
After I created an array with operations which will handle (process and update database) JSON that I pulled from the server:
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{
//first we need to tether countries, regions and cities
[[PTDataTetherer sharedInstance] tetherCountriesRegionsCitiesInContext:self.updateContext];
//generating fake agencies
//[PTFakeAgencyGenerator generateAgenciesInContext:context];
//generating fake clients
//[PTFakeClientGenerator generateClientsInContext:context];
//generating fake reports
[[PTFakeReportGenerator sharedInstance] generateReportsInContext:self.updateContext];
//generating fake presentations
[[PTFakePresentationGenerator sharedInstance] generatePresentationsInContext:self.updateContext];
//tethering
[[PTDataTetherer sharedInstance] tetherAgenciesWithOthersInContext:self.updateContext];
[[PTDataTetherer sharedInstance] tetherClientsWithOthersInContext:self.updateContext];
[[PTDataTetherer sharedInstance] tetherEventsWithOthersInContext:self.updateContext];
[[PTDataTetherer sharedInstance] tetherPresentationFoldersWithImagesInContext:self.updateContext];
[self saveContext];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"kSynchronizationFinishedNotification" object:nil];
}];
}];
[op setQueuePriority:NSOperationQueuePriorityLow];
if ([handleJSONOperations count]==0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"kSynchronizationFinishedNotification" object:nil];
}];
}
else
{
[self.serverUpdateQueue addOperation:updateContextCreateOperation];
[handleJSONOperations addObject:op];
[self.serverUpdateQueue addOperations:handleJSONOperations waitUntilFinished:NO];
};
Basically I want to construct the queue in such way:
1. [context create operation]
2. [multiple context modify operations that will parse json received from server and save new/modify objects to/in context]
3. [some final methods that will also modify context and, at the end, that will call a save method to propagate changes to the storage and then using NSManagedObjectContextDidSaveNotifications to other context]
Is it safe to call singletons methods in block?
It's a bit board question, depends on what you inside your singleton's method.
Is it safe to call class methods in block?
Depends on what you do inside your method. From my experience and the code I do, yes.
Is it save to call "self" methods?
You are passing a reference of self to the block, which might cause a memory leak.
I have some code that sends multiple ASIHTTPRequests to upload and download data in a view controller. When the view controller gets dealloc'd it should clean up all unfinished requests by setting the delegate to nil.
- (void)viewDidLoad
{
// send multiple requests
[self sendRequest:someURL];
[self sendRequest:someURL];
[self sendRequest:someURL];
[self sendRequest:someURL];
}
- (void)sendRequest:(NSString*)url
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
ASINetworkQueue *requestQueue = [ASINetworkQueue queue];
[requestQueue setMaxConcurrentOperationCount:2];
[requestQueue setDelegate:self];
[requestQueue addOperation:request];
[requestQueue go];
}
- (void)dealloc
{
NSLog(#"cancel all operations");
for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)
{
[req cancel];
[req setDelegate:nil];
}
[super dealloc];
}
However, if I pop this view controller before all operations have finished, I get a "message sent to deallocated instance" in ASIHTTPRequest.m complaining that the delegate went away in the code below.
/* ALWAYS CALLED ON MAIN THREAD! */
- (void)reportFailure
{
***crash here --> if (delegate && [delegate respondsToSelector:didFailSelector]) {
[delegate performSelector:didFailSelector withObject:self];
}
if (queue && [queue respondsToSelector:#selector(requestFailed:)]) {
[queue performSelector:#selector(requestFailed:) withObject:self];
}
#if NS_BLOCKS_AVAILABLE
if(failureBlock){
failureBlock();
}
#endif
}
How can I work around this?
You're creating a new queue for each request around this line of code:
ASINetworkQueue *requestQueue = [ASINetworkQueue queue];
So the loop here won't loop over the requests as it's looping over the sharedQueue, not the new one(s) you've created:
for (ASIHTTPRequest *req in ASIHTTPRequest.sharedQueue.operations)
Requests would only get added to the sharedQueue if you use [request startAynchronous] without explicitly setting a different queue.
I may be missing something, but I think waiting until dealloc is too late, you want to cancel your operations on viewWillDisappear or viewDidUnload
I'm writing test cases for a wrapper class written around ASIHTTPRequest. For reasons I can't determine, my test cases complete with failure before the ASIHTTPRequest finishes.
Here's how the program flow works.
Start in my test case.
Init my http engine object, instruct it to create a new list
Create the new ASIHTTPRequest object and set it up.
Add the request to an operation queue.
Wait until that queue is empty
Check to see if my delegate methods were called and fail the test if they weren't.
Now, most of the time everything works fine and the test passes, but some of the time it fails because my delegate methods were called AFTER the operation queue returned control to my wait method.
Test Case
// Set my flags to 'NO'
- (void)setUp {
requestDidFinish = NO;
requestDidFail = NO;
}
- (void)testCreateList {
NSString *testList = #"{\"title\": \"This is a list\"}";
JKEngine *engine = [[JKEngine alloc] initWithDelegate:self];
NSString *requestIdentifier = [engine createList:jsonString];
[self waitUntilEngineDone:engine];
NSString *responseString = responseString_;
[engine release];
GHAssertNotNil(requestIdentifier, nil);
GHAssertTrue(requestDidFinish, nil);
GHAssertTrue([responseString hasPrefix:#"{\"CreateOrEditListResult\""], nil);
}
// Puts the test into a holding pattern until the http request is done
- (void)waitUntilEngineDone:(JKEngine *)engine {
[engine waitUntilFinishedRunning];
}
// The delegate method called on successful completion
- (void)requestFinished:(NSString *)requestIdentifier withResponse:(NSString *)response {
NSLog(#"request did finish");
requestDidFinish = YES;
responseIdentifier_ = [requestIdentifier retain];
responseString_ = [response retain];
}
Engine Code
- (NSString *)createList:(NSString *)list {
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request addRequestHeader:#"Content-Type" value:kContentType];
[request setRequestMethod:kPOST];
request.delegate = self;
[request appendPostData:[list dataUsingEncoding:NSUTF8StringEncoding]];
NSString *requestIdentifier = [NSString stringWithNewUUID];
[operationQueue_ addOperation:request];
[operationDictionary_ setObject:request forKey:requestIdentifier];
return requestIdentifier;
}
// This is the ASIHTTPRequest delegate method that's called on success
// but it sometimes isn't called until AFTER the operationQueue finishes running
- (void)requestFinished:(ASIHTTPRequest *)request {
DLog([request responseString]);
BOOL canNotifiyDelegate = [self.delegate respondsToSelector:#selector(requestFinished:withResponse:)];
if (canNotifiyDelegate) {
NSArray *keyArray = [operationDictionary_ allKeysForObject:request];
NSString *requestIdentifier = [keyArray objectAtIndex:0];
[operationDictionary_ removeObjectForKey:requestIdentifier];
if ([keyArray count] != 1) {
ALog(#"It looks like a request was added to the operation dictionary multiple times. There's a bug somewhere.", nil);
}
[self.delegate requestFinished:requestIdentifier withResponse:[request responseString]];
}
}
- (void)waitUntilFinishedRunning {
[operationQueue_ waitUntilAllOperationsAreFinished];
}
This is the way ASIHTTPRequest works. Delegate methods are called on the main thread, and calls to delegates do not block the request thread, so it's perfectly possible your delegates will be called after the queue finishes.
ASIHTTPRequest calls delegate methods on the main thread, by default GH-Unit runs its tests on a background thread. I'm still a little hazy on exactly what was going on, but forcing my network tests to run on the main thread fixed the problem.
I implemented the following method in my network test class.
- (BOOL)shouldRunOnMainThread {
return YES;
}
I hope to get the http header info(file size) in asynchronous mode.
So I initialize as codes:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(processReadResponseHeaders:) name:#"readResponseHeaders"
object:nil];
my codes to read the http header
-(void)processReadResponseHeaders: (ASIHTTPRequest *)request ;//(id)sender;
{
unsigned long long contentLength = [request contentLength]; //error occurs here
}
It has to change the source code of ASIHTTPRequest.m
I did add my codes in function readResponseHeaders to notify the event is triggered )
- (void)readResponseHeaders
{
//.........................
[[NSNotificationCenter defaultCenter] postNotificationName:#"readResponseHeaders" object:self];//
}
the log file reports:
2010-05-15 13:47:38.034 myapp[2187:6a63] *** -[NSConcreteNotification contentLength]: unrecognized selector sent to instance 0x46e5bb0
Welcome any comment
Thanks
interdev
NSNotificationCenter's observers' selector must have one of the signatures:
-(void)observe;
-(void)observeWithNotification:(NSNotification*)notification;
It cannot be an ASIHTTPRequest (even if you put ASIHTTPRequest* in the argument, it is still an NSNotification.)
There are 3 properties of NSNotification: name, object and userInfo. You could obtain the ASIHTTPRequest with object, if self is an ASIHTTPRequest when you post that notification:
-(void)processReadResponseHeaders:(NSNotification*)notification {
ASIHTTPRequest* request = [notification object];
unsigned long long contentLength = [request contentLength];
...
}