I can't get rid of a EXC_BAD_ACCESS error in RestKit. I suspect its because I have an ARC project and may be releasing the request or response variable too many times, but I've spent hours on this and am not sure.
My problem sounds similar to this post, but I'm not sure where in my code to make a similar change.
My implementation file has a straightforward method to post the new object to the server. All the mapping logic is down within the implementation file for the NSObject below:
-(void) createMeeting
{
NSString* baseUrl = #"https://myapp.appspot.com/api/meeting/?format=json&username=testuser#test.com&api_key=f8s9df8as8df9s8d97";
RKObjectManager* rkoManager = [RKObjectManager objectManagerWithBaseURLString:baseUrl]; [NewMeetingRK initMap:rkoManager];
NewMeetingRK *newmtg = [NewMeetingRK alloc];
newmtg.leader = self.leaderEmail.text;
newmtg.startdate = [sqliteformatter stringFromDate:bdate];
newmtg.enddate = [sqliteformatter stringFromDate:edate];
[[RKObjectManager sharedManager] postObject:newmtg delegate:self];
And it successfully begins requestDidStartLoad:(RKRequest *)request
However it then crashes in RKResponse.m on the second to last line below (if ([[_request delegate] respondsToSelector:... with a EXC_BAD_ACCESS error.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
RKResponseIgnoreDelegateIfCancelled();
RKLogDebug(#"NSHTTPURLResponse Status Code: %ld", (long) [response statusCode]);
RKLogDebug(#"Headers: %#", [response allHeaderFields]);
_httpURLResponse = [response retain];
[_request invalidateTimeoutTimer];
if ([[_request delegate] respondsToSelector:#selector(request:didReceiveResponse:)]) {
[[_request delegate] request:_request didReceiveResponse:self];
}
}
Any ideas to help me? Thanks much.
EXC_BAD_ACCESS happens when a message is sent to an object that has been released.
You should pay attention to _request delegate object. NSZombieEnabled break point might help you too. How to enable zombie objects
Related
I have an app with four tabs. In each tab I connect to remote server using nsurlconnection, fetches the response and display accordingly. While testing the app, I get crashes randomly. If I try to reproduce the crash again I do not get crash. I do not understand what is root cause of the crash. I enable NSZombie,symbolicated crash logs,checked the memory leak but no luck.
I started the project in Xcode 3 and now I imported same project to Xcode 4.2, so are there any issues with compatibility of Xcode?
And I use the same name for nsurlconnection in all tabs like
In Tab 1 I defined nsurlconnection as conn and Tab 2 defined nsurlconnection as conn.
Does this definition causes any issue?
Please help me solve this random crashes
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(label != nil){
progressView = [[ProgressView showHUDAddedTo:self.tabBarController.view animated:YES] retain];
progressView.labelText = label;
}
[request release];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(#"didReceiveresponse");
if ([response isKindOfClass: [NSHTTPURLResponse class]]) {
if([(NSHTTPURLResponse *)response statusCode] == 200){
}
else{
//show Connection Error Alert
}
}
responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"didReceiveData");
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[progressView hide:YES];
NSLog(#"didFail");
//show failed alert
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"didfinish loading");
if([responseData length] > 0)
{
//handles response data
}
}
My guess without seeing code, would be that in a tab your making an NSURLConnection and doing something with the result when it completes. If you change tab before the result is returned then it is causing it to crash.
You need to cancel the NSURLConnection when viewDidDisappear, or make sure that whatever code is run on completion of it doesn't contain anything that would cause a crash if the tab isn't visible (like setting a labels text).
The way I handle this, is have a separate class that performs the URL requests that sends a notification when it's complete. That way in your viewDidAppear method you set your viewController to listen for the notifications, and in the viewDidDisapper method you stop listening for the notifications. So if your view isn't visible when the URL request finishes, the notification is fired but nothing happens.
Could you provide the output of the console? It seems not to be an error from Xcode.
These type of error usually appear when you try to access a deallocated object.
i am sure you have tried Instruments with memory leak. give a try with instruments with zombie tool, you can find it easily in instruments library.
run your code with this tool and if this crash is because of any zombie object then you will easily able to detect the location.
it has helped me few times.
hi in one of my application. i have to send a request to the server (json server) many times continously.my url will be like this as mention below
#"http://185.185.116.51/servername/serverjspfilesname.jsp?filterID=21&ticket=65675656565656567"
actually i have many filter id's (filter id you can find at top).in order to chnage the filterid continously i used for loop like this as mention below
for(int i=0;i<[appdelegate.listOfFiltersArray count];i++)
{
filtersDataModelObject=[[ListOfFiltersDataModel alloc]init];
filtersDataModelObject=[appdelegate.listOfFiltersArray objectAtIndex:i];
homescreenstring=[NSString stringWithFormat:#"http://%#/servername/serverjspfilesname.jsp?filterID=%#&ticket=%#",Ip,filtersDataModelObject.filterID,[storeData stringForKey:#"securityTicket"]];
NSLog(#"url is %#",homescreenstring);
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:homescreenstring]];
connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection)
{
homeScreenResponseData=[[NSMutableData alloc]init];
}
else
{
NSLog(#"connection failed");
}
}
actually after each condition is satisfied in for loop i have to connect with the server for getting the data from the server using nsurlconnection delegate methods. but here after complete execution of for loop only nsurlconnection delegate methods are executing with last filterid which is getting from the appdelegate.listOfFiltersArray array.
but i would like to call the server for each filterid.
if anyone know please let me know.thanks in advance.
Create one count variable int count in .h file.
int count = 0 //in .m file
Now use this method:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//one request finished
count ++;
//request another using count
}
The solution that Prince proposed is not general as you will face the problem of defining the
"filterid" for each request.
And what you are doing is a bad approach. Dont mix the web request with your business logic code.The web stuff should be handled by a separate file handling all the requests throughout the app. And that class will implement delegation.
For delegation you need to do the following.
In your Network class header (Networkclass.h) add the protocol
#protocol NetworkControllerDelegate <NSObject>
-(void) UrlResponseRecieved :(NSData *)responseData;
-(void) UrlResponseFailed:(NSError *)error;
#end
and in NetworkClass.m (implementation fie) do the following
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (receivedData != nil)
{
if([delegate respondsToSelector:#selector(UrlResponseRecieved:)])
[self.delegate UrlResponseRecieved:receivedData];
[receivedData release];
receivedData = nil;
}
[connection release];
}
if you still get stuck you may refer to the following
What's wrong on following URLConnection?
or you may read any asynchronous downloading tutorial first.
First of all the questions are failry simiple.. if you just want to see what they are skip to the bottom of this post and you will see them in bold.. for more detail then you can read the rest of this post...
I am just trying to iron out my NSURLConnection so that its working smoothly and I understand this properly. There is a profound lack of example/tutorials for Asynchronous connections on the internet or not any that I can find that explaine what is going on with any level of depth other than getting the connection up and running which after working on it seems pretty simple. Hopefully this question can full the void that I feel is out there for other users.
So, in my .h file i have imported the foundations headers and declared the methods required for the received or lack of received data (errors etc).
.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h> //add foundations
//.. other headers can be imported here
#interface MyViewController: UITableViewController {
//Im not setting any delegates to access the methods because Is all happening in the same
//place so I just use the key word 'self' when accessing the methods declared below
//I'm not sure if this is the best thing to do but I wasn't able to get my head around declaring the delegate or how it would help me with the way I have set up my request etc.
}
- (IBAction)setRequestString:(NSString *)string; //this method sets the request and connection methods
//these methods receive the response from my async nsurlconnection
- (void)receivedData:(NSData *)data;
- (void)emptyReply;
- (void)timedOut;
- (void)downloadError:(NSError *)error;
So thats my header file.. pretty simple not much explaining needed.
.m
//call setRequestString from some other method attached to a button click or something
[self setRequestString:#"rss.xml"];
//..
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:#"http:www.comicbookresources/feeds/"]; // address not real jsut example
//append the string coming in to the end of the databaseURL
[databaseURL appendString:string];
//prepare NSURL with newly created string
NSURL *url = [NSURL URLWithString:databaseURL];
//AsynchronousRequest to grab the data
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil){
[self receivedData:data];
}else if ([data length] == 0 && error == nil){
[self emptyReply];
}else if (error != nil && error.code == NSURLErrorTimedOut){ //used this NSURLErrorTimedOut from foundation error responses
[self timedOut];
}else if (error != nil){
[self downloadError:error];
}
}];
}
now set up the methods that were initialized in the .h file and called in the if statement above
- (void)receivedData:(NSData *)data
{
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"%#", newStr); //logs recived data
//now you can see what data is coming in on your log
//do what you want with your data here (i.e. start parsing methods
}
- (void)emptyReply
{
//not sure what to do here yet?
}
- (void)timedOut
{
//also not sure what to do here yet?
}
- (void)downloadError:(NSError *)error
{
NSLog(#"%#", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"A connection failure occurred." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
Cool so that pretty much the basics of what I have done right there.. now the questions I have are as follows.
Question one:
Where I call NSURLConnection like so
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
What is happening here what is the ^ for is that executing that whole block (including the if statements) on a different thread or something? because it looks alot like grand central dispatch formatting but slightly different.
Question two:
what should I be doing inside emptyReply & timedOut methods?
Question three:
How would I incorporate caching into this? I would like to cache the responses I get back from different requests. i.e. with my setRequestString you will see there is a string input parameter, so i can request different rss feeds with the same method.. I need to figure out how to cache these responses into individual caches.. but im not sure where to start with it.
Finally
If you have made it this far, thank you very much for reading my question. Hopefully with your responses we can get a pretty nice solution going here.. that other people can use for themselves and pick and choose the bits and peices they need that works for there own solution..
Anyway thank you very much for reading and I look forward to your replies.. even if they are just refrences to tutorials or examples you think might help me.. anything is good I just want to fully understand whats going on and whats a good solution.
Read about blocks in Apple documentation. Its new. Or you can read here
You can show errors such as request timed out etc. You don't really have to handle them separately than the error one unless you have special logic.
Try this for caching
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
I wanted help from you guys. I have implemented the storekit code in my iPhone/iPad app and I am testing the application on iPad 1 with iOS 3.2.
I tried to test the application after performing all the steps like adding the products in for an application in iTunes connect and using provisioning profile to run that app on my iPad but when i run the application Storekit delegate functions are never called neither it gives any error and it never crashes. I can't figure out what is the problem.
Please help me to resolve this.
Below is the code which i have used
- (void) requestProductData
{
SKProductsRequest *request= [[SKProductsRequest alloc]
initWithProductIdentifiers: [NSSet setWithObject:#"myproductid"]];
request.delegate = self;
[request start];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
// populate UI
[request autorelease];
}
- (void) request:(SKRequest *)request didFailWithError:(NSError *)error {
NSString *errorMessage = [error localizedDescription];
NSLog(#"%#",errorMessage);
}
- (void)requestDidFinish:(SKRequest *)request{
NSLog(#"%#",#"inside request finish");
}
I call the requestProductData but none of the delegate function is called.
Thanks so much in advance!
I figure this is a bit late and you would of solved it by now, but I am trying to resolve another issue, came across your question and was wondering if you had the delegates in your interface file.
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#interface ViewController : UIViewController<SKProductsRequestDelegate, SKRequestDelegate, SKPaymentTransactionObserver>
#end
I suspect you did have the above code and as said solve it, but just in case someone else has the same issue and has forgotten to include the delegates in the h file.
how i get the URL inside the following method ??
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
Hey there's a comment from Mihai Damian that worked for me:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSURL *myURL = [[connection currentRequest] URL];
Cheers
You ought to be able to do theConnection.request.URL, but you can't. Annoying, isn't it?
The simplest way is to just save the URL (or the whole NSURLRequest) that you were loading. If you're using multiple connections, you can store them in a dictionary. Note that -[NSMutableDictionary setObject:forKey:] copies keys, and NSURLConnections are not copyable; the workaround is to use CFDictionarySetValue instead:
CFDictionarySetValue((CFMutableDictionaryRef)dict, connection, request);
Of course the above answers work, and I am looking for similar solution.
Just found that NSLog([connection description]); prints something like:
< NSURLConnection: 0x9129520, http://google.com>
So it is possible to parse the string returned by [connection description], and get the url from the connection, though it is kind of dirty.
You can get URL like this
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
NSLog(#"Connection failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
for more information you van read here.
Here is my suggestion
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.rssFeedConnection = nil;
NSLog(#"connectionDidFinishLoading url : %# ", connection.originalRequest.URL);
}
In Swift 2.0 iOS 9 you can do it like:
func connectionDidFinishDownloading(connection: NSURLConnection, destinationURL: NSURL) {
print(connection.currentRequest.URL!)
}