Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sending Functions and Code Through Main Thread?

rebello95rebello95 Posts: 276Registered Users @ @
edited July 2013 in iOS SDK Development
I'm putting together a program that connects to a Google Calendar and receives a callback, sending it to another selector. The problem I'm having is extremely similar to one at this link. I attempted to follow the solution, but I can't seem to get it right. I tried putting it in the following method, but it really didn't seem to make a difference so I undid it. Any help would be much appreciated because I've spent hours on this and am being absolutely driven insane.
- (id)initWithCoder:(NSCoder *)aCoder{
  if( self=[super initWithCoder:aCoder] ){
    googleCalendarService = [[GDataServiceGoogleCalendar alloc] init];
    [googleCalendarService setUserAgent:@"Demo-1.0"];
    // We'll follow the links ourselves, so that we can show progress to our users between each batch.
    [googleCalendarService setServiceShouldFollowNextLinks:NO];
    data = [[NSMutableArray alloc] init];
  }
  return self;
}
Post edited by rebello95 on

Replies

  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    The link you posted just asks if the OP was creating the calendar request and servicing it on the same thread. There isn't a specific solution other than that.

    Are you running code on another thread?

    You also don't show any code that uses a selector at all.

    In short, your post doesn't provide enough information for anybody to help you.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • rebello95rebello95 Posts: 276Registered Users @ @
    edited July 2013
    I used NSLog to determine that it is supposedly running everything on the same thread, so that's not it. Here are the functions that are supposed to be getting called:
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem  alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                                                                               target:self
                                                                                               action:@selector( refresh )];
        // At this point, the application delegate will have loaded the app's preferences, so set the service's credentials.
        PNDAppDelegate *appDelegate = [PNDAppDelegate appDelegate];
        [googleCalendarService setUserCredentialsWithUsername:appDelegate.username
                                                     password:appDelegate.password];
        [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO];
    }
    
    -(void)refresh {
      // Note: The next call returns a ticket, that could be used to cancel the current request if the user chose to abort early.
      // However since I didn't expose such a capability to the user, I don't even assign it to a variable.
        [data removeAllObjects];
        
        //Set current date
        currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem());
        
        //THIS IS THE METHOD THAT ISN'T GETTING CALLED
        [googleCalendarService fetchCalendarFeedForUsername:[PNDAppDelegate appDelegate].username delegate:self didFinishSelector:@selector(calendarsTicket:finishedWithFeed:error:)];
        NSLog(@"Thread: %@", [NSThread currentThread]);
    }
    
    - (void)calendarsTicket:(GDataServiceTicket *)ticket finishedWithFeed:(GDataFeedCalendar *)feed error:(NSError *)error{
        NSLog(@"Success!");
    }
    I migrated the code from the NavigationController style demo app (gCal) into a Single-View style app... But I don't think that could cause the issue, could it?

    Thanks!
    Post edited by rebello95 on
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    rebello95 said:

    I used NSLog to determine that it is supposedly running everything on the same thread, so that's not it. Here are the functions that are supposed to be getting called:
    - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = self.editButtonItem; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector( refresh )]; // At this point, the application delegate will have loaded the app's preferences, so set the service's credentials. PNDAppDelegate *appDelegate = [PNDAppDelegate appDelegate]; [googleCalendarService setUserCredentialsWithUsername:appDelegate.username password:appDelegate.password]; [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:NO]; } -(void)refresh { // Note: The next call returns a ticket, that could be used to cancel the current request if the user chose to abort early. // However since I didn't expose such a capability to the user, I don't even assign it to a variable. [data removeAllObjects]; //Set current date currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); //THIS IS THE METHOD THAT ISN'T GETTING CALLED [googleCalendarService fetchCalendarFeedForUsername:[PNDAppDelegate appDelegate].username delegate:self didFinishSelector:@selector(calendarsTicket:finishedWithFeed:error:)]; NSLog(@"Thread: %@", [NSThread currentThread]); } - (void)calendarsTicket:(GDataServiceTicket *)ticket finishedWithFeed:(GDataFeedCalendar *)feed error:(NSError *)error{ NSLog(@"Success!"); }

    I migrated the code from the NavigationController style demo app (gCal) into a Single-View style app... But I don't think that could cause the issue, could it?

    Thanks!

    How does an NSLog statement let you "...determine that it is supposedly running everything on the same thread"?
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • rebello95rebello95 Posts: 276Registered Users @ @
    NSLog(@"Thread: %@", [NSThread currentThread]); should give you the current thread, right?
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    rebello95 said:

    NSLog(@"Thread: %@", [NSThread currentThread]); should give you the current thread, right?

    That will give you the thread that's executing at the time the log statement runs, yes. However, how do you interpret it?
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • rebello95rebello95 Posts: 276Registered Users @ @
    Well, I assumed that by running this right before and right after the function calls the selector that it would give me the thread that the selector is running on. Putting it before says that it's on thread 1 (main thread?), and then the selector doesn't get the callback.
  • rebello95rebello95 Posts: 276Registered Users @ @
    edited December 2012
    Ok, at last I managed to fix it. It turns out that the problem had nothing to do with code! The issue was, as I suspected, related to the navigation system. I created a new project with storyboards and nested it under a NavigationController. I then pushed the calendar's VC, and it works fine. Still not quite sure what the exact issue was, but that fixed it. Thanks for your help, Duncan!
    Post edited by rebello95 on
Sign In or Register to comment.