Chat - Users list not showing up, reporting error - quickblox

I rebuilt the chat app example from scratch and everything was going along correctly until I try to load the users list. As I try to load, no names show and an alert view pops up with just 'error.' Any question on how to solve this? I used almost the exact code from the chat example. Here's what I did:
#implementation UsersViewController
- (void)viewDidLoad
{
[super viewDidLoad];
QBUUser *user = [QBUUser user];
[QBUsers signUp:user delegate:self];
[QBAuth createSessionWithDelegate:self];
[QBUsers logInWithUserLogin:#"%#" password:#"%#" delegate:self];
[QBUsers userWithExternalID:46732 delegate:self];
self.users = [NSMutableArray array];
self.selectedUsers = [NSMutableArray array];
self.paginator = [[UsersPaginator alloc] initWithPageSize:10 delegate:self];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// Fetch 10 users
[self.paginator fetchFirstPage];

You can't do it this way:
[QBUsers signUp:user delegate:self];
[QBAuth createSessionWithDelegate:self];
[QBUsers logInWithUserLogin:#"%#" password:#"%#" delegate:self];
[QBUsers userWithExternalID:46732 delegate:self];
all these requests are asynchronous, not synchronous
you have to wait until each request will be finished in order to perform next one

Related

NSURL Caching issues

I'm having an issue with a login API. First call works fine, but subsequent calls are cached. This is causing an issue since login/logout functionality is essentially broke.
I've tried many methods and I'm implementing AFNetworking library.
In AppDelegate.m:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
diskCapacity:0
diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
In my Networking class:
(AFHTTPRequestOperation *)createRequestOperationWithMethod:(NSString *) method andPath: (NSString *)path andParams:(NSDictionary *)params
{
GRAPIClient *httpClient = [GRAPIClient sharedClient];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:method
path:path
parameters:params];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData]
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
return operation;
}
I even tried to overwrite the request being generated in AFHTTPClient
In AFHTTPClient.m:
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval:2.0];
My GRAPIClient implementation:
#interface GRAPIClient : AFHTTPClient
+ (GRAPIClient *)sharedClient;
+ (BOOL) isInternetReachable;
#end
#implementation GRAPIClient
+ (BOOL) isInternetReachable
{
return reachable;
}
+ (GRAPIClient *)sharedClient {
static GRAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[GRAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFAppDotNetAPIBaseURLString]];
});
[_sharedClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == AFNetworkReachabilityStatusReachableViaWWAN ||
status == AFNetworkReachabilityStatusReachableViaWiFi ) {
NSLog(#"Reachable on!");
reachable = YES;
}
else
{
NSLog(#"Reachable off!");
reachable = NO;
}
}];
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:#"Accept" value:#"application/json"];
return self;
}
#end
I've debugged responses from the server and tested with hard coding two NSURLRequests simultaneously to the server. One for User A and one for User B, then printed the response data for both users.
On first login, User A login returned User A credentials. User B returned User B credentials. On second login, User A returned User A credentials, User B returned User A credentials. I have no idea how to fully disable cacheing.
Try:
[operation setCacheResponseBlock:^NSCachedURLResponse*(NSURLConnection* connection, NSCachedURLResponse* cachedResponse) {
return nil;
}];
And:
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
The issue for me as suggested by SixteenOtto was a session being sent from the server and AFNetworking automatically using the cookie. I hadn't considered this before since we're using a restless API based on auth tokens, so a cookie for the session makes no sense. However, inspecting the HTTP response headers with Charles allowed me to see this.
Adding
[request setHTTPShouldHandleCookies:NO];
To my operation generator solved the issue.

Spinning wheel blocks lazy image load

It sounds a bit strange, but since i use a spinning wheel indicator, the lazy image load don't works for the first image views (these once that are shown in the first screen). If the user scrolls down all other Images in the TableView loading correctly by a lazy download.
The main problem is, that NSURLConnection didn't calls didReceiveData.
- (void)startDownload
{
self.activeDownload = [NSMutableData data];
BOOL firstCell = (self.indexPathInTableView.row==0 && self.indexPathInTableView.section==0);
if(firstCell){
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:newsContent.title_picture]] delegate:self];
NSLog(#"Get Title Pic %# (%#)",newsContent.title, newsContent.title_picture);
self.imageConnection = conn;
}else{
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:newsContent.cover_picture]] delegate:self];
NSLog(#"Get Thumb Pic %# (%#)",newsContent.title, newsContent.cover_picture);
self.imageConnection = conn;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(#"[NewsPicture][connection]didReceiveData");
[self.activeDownload appendData:data];
}
Edit: Added didReceiveResponse
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"[NewsPicture][connection]didReceiveResponse");
}
I'll get the Log "Get Thumb Pic ... (...)" with a correct Url, but for the for the first 5 rows (they fills the screen of an iPhone 4) i don't get the Log "[NewsPicture][connection]didReceiveData".
This is the way how i call the Indicator:
// Spinning Wheel
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.tag = 1000;
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"wird geladen";
HUD.minShowTime = 25;
HUD.dimBackground = YES;
[HUD show:true];
[HUD showWhileExecuting:#selector(doWhileLoadingNews) onTarget:self withObject:nil animated:NO];
and if i only call
[self doWhileLoadingNews];
at this place all works fine, but without in indicator for loading data.
How could i fix it? (I can post more Code oder Informations if you need)
Edit: I still couldn't fix it. Is it possible to catch the result in another way then calling the 'didReceiveData'?
Edit: Added didReceiveResponse but with the same result, didReceiveResponse is also not called.

View Controller Segue Delay after NSURLConnection

So this is my first post, I've found this site incredibly informative in my brief history with Objective C and iOS programming. Anyhow, I've run into a problem of sorts. A quick summary: I'm attempting to write a login form, which uses calls a custom class that with hit a webserver to auth using NSURLConnection. I'm using protocols and delegates to delegate back to the calling class to perform a segue to the main menu view controller once the authentication is complete.
The problem is that the menu I'm attempting to segue into takes anywhere from 6 to 75 seconds to display. If I remove the API call, it loads immediately. However, I'm doing logging throughout the process, and everything appears to step through at a normal pace. I even log when the menu view controller is loaded, and all logging happens normally. But the actual display of the menu is delayed!
Here are some code details:
View Controller Methods:
- (void) userLogin:(NSString *)userName password:(NSString *)password {
NSLog(#"VC login method");
api = [theAPI getSelf];
[api setDelegate:self];
[api userLogin:userName password:password];
}
- (void) userLoginDone:(BOOL)successful {
[self performSegueWithIdentifier:#"sgLoginToMainMenu" sender:self];
NSLog(#"Login Done");
}
API Method:
- (void) userLogin:(NSString *)userName password:(NSString *)password {
NSURL *url = [NSURL URLWithString:(NSString *) [API_PATH stringByAppendingString:#"test.html"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *json = [[JSON new] parseJSON:data];
self.usrID = [json objectForKey:#"usrID"];
self.sessionID = [json objectForKey:#"sessionID"];
self.userName = [json objectForKey:#"Username"];
NSLog(#"Username: %#", [json objectForKey:#"Username"]);
[[self delegate] userLoginDone:YES];
}];
}
All the NSLogs execute in a normal timespan (few milliseconds). Yet the main menu view controller takes entirely too long to appear! I'm very new to iOS programming, so I'm hoping I'm just overlooking something that googling couldn't solve. Any help would be greatly appreciated!
You need to update the UI on the main thread, but userLoginDone: is being called on an NSOperationQueue, which create its own separate thread. This could explain the delay in displaying. Have you tried using [NSOperationQueue mainQueue] (which returns the queue associated with the main thread) to pass to sendAsynchronousRequest: instead?

NSURLConnection is not calling didFailWithError.

I am attempting to write a bit of code that checks the URL of a datasource, then populates an array with objects from that URL. It actually works well, but if there is a problem with the web connection or the address I want to populate the array with data from a bundled file. The issue I am having is that the connection didFailWithError method is never called. I tried passing a simple string but it does not call. I want the app to still function for people who are using ipod touch or are in airplane mode.
connection didReceiveResponse is working without issue.
This is what I'm working with.
- (void)loadListData{
NSLog(#"Loading data from sources");
NSURLRequest *listURLRequest = [NSURLRequest requestWithURL:integerPhoneListURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1.0];
[[NSURLConnection alloc] initWithRequest:listURLRequest delegate:self];
if (!listConnectFail){
phoneListJSON =[NSData dataWithContentsOfURL:integerPhoneListURL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:phoneListJSON waitUntilDone:YES];
} else {
//This will tell us if there is an error loading the file
NSLog(#"File not found on web init from file");
phoneListJSON =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"contactlist" ofType:#"json"]];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:phoneListJSON waitUntilDone:YES];
}
//Initialize the filtered list with array of customer objects. Based on original data
filteredList = [[NSMutableArray alloc] init];
for (NSDictionary *dict in phoneListOriginal) {
contact *single = [[contact alloc] init];
single.fName = [dict objectForKey:#"fName"];
single.lName = [dict objectForKey:#"lName"];
single.extension = [dict objectForKey:#"extension"];
single.title = [dict objectForKey:#"title"];
single.department = [dict objectForKey:#"department"];
single.cellNumber = [dict objectForKey:#"cellNumber"];
//NSLog(#"%#", single.lName);
[filteredList addObject:single];
}
NSLog(#"Array filteredLIst contains %d records",[filteredList count]); }
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
listConnectFail = YES;
NSLog(#"Connection Failed, pulling from file"); }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
listConnectFail = NO;
NSLog(#"Connection Succeeded, populating from API");
}
I know it is probably something stupid that I am not seeing, but I could use the help to see what I don't
Thanks in advance!
How did you confirm that your delegate did not receive the message? Did you check the log?
Your code seems to assume that 'listConnectFail' will be set immediately after the NSURLConnection's init is done, which is not necessarily the case.
[[NSURLConnection alloc] initWithRequest:listURLRequest delegate:self];
if (!listConnectFail){...}
The NSURLConnection documentation states that 'The delegate will receive delegate messages as the load progresses.'
However, I am not sure about the airplane mode, maybe this particular error can be detected synchronously.

Can you set the tableView datasource values in "connectionDidFinishLoading"?

I'm working on my first JSON example in objective-c and came across this great tutorial that I'm trying to reproduce. Along the way I decided to push the JSON returned into my already working tableView (just to ensure I could do something w/ the data in the view).
- (void)viewDidLoad {
[super viewDidLoad];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.unpossible.com/misc/lucky_numbers.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSArray *luckyNumbers = [responseString JSONValue];
NSMutableString *text = [NSMutableString stringWithString:#"Nums "];
for (int i = 0; i < [luckyNumbers count]; i++)
[text appendFormat:#"%#", [luckyNumbers objectAtIndex:i]];
self.movies = [[NSArray alloc] initWithObjects:#"First", text, #"Last", nil];
}
What I've found is that when I set the array in "connectionDidFinishLoading" it shows up as nothing in the running application - yet if I set this directly in the "viewDidLoad" method with 3 simple string values it shows up fine.
When I debug the running application I see the JSON response and the string looks valid (no issues that I can see).
Is the datasource for my tableView already set in stone before this "connectionDidFinishLoading" method or did I miss something?
Your UITableView will call upon its DataSource for data once initially, presumably sometime after viewDidLoad. After that first load, it will only request data as it needs it (i.e. as you scroll to different cells.) If you want to make it refresh its contents when your data is ready (like after you've received your URL data), call [tableView reloadData].
My initial question was solved by this solution:
At the end of my "connectionDidFinishLoading" method I call a method on the appDelegate called "jsonFinished".
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//do all the json work and set the array that I'm using as my datasource
self.movies = [[NSArray alloc] initWithObjects:#"First", "Last", nil];
[appDelegate jsonFinished]; //have the app delegate do the refresh call back
}
Then inside the appDelegate I simply provide an implementation for the "jsonFinished" method that does a refresh of the UITableView
- (void)jsonFinished
{
moviesController.refreshDisplay;
}
And in the "refreshDisplay" method I do the reloadData on the tableView
- (void)refreshDisplay
{
[moviesTableView reloadData];
}
And now after the data is loaded the appDelegate fires off the method that reloads the data for tableView