I'm experiencing a strange issue that only happens intermittently when I run my app. I'm attempting to pull down JSON from two different sources, using AFNetworking. Occasionally, when the operations are running, the app will crash with *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' landing on json_request_operation_processing_queue.
I'm hoping this isn't a problem with AFNetworking, and I'm just doing something incorrectly. Here are my methods that I think are relavent (JSONManager extends AFHTTPClient):
+ (JSONManager *) sharedJSONManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedJSONManagerInsance = [[JSONManager alloc] initWithBaseURL:[NSURL URLWithString:sourceUrl1]];
});
return _sharedJSONManagerInsance;
}
- (void) loadOperations {
_sharedJSONManagerInsance.operations = [NSMutableArray arrayWithCapacity:2];
[_sharedJSONManagerInsance.operations addObject:[self fetchJSON:sourceUrl1]];
[_sharedJSONManagerInsance.operations addObject:[self fetchJSON:sourceUrl2]];
}
- (void) executeOperations {
[_sharedJSONManagerInsance loadOperations];
_sharedJSONManagerInsance.fetchedStories = [[NSMutableArray alloc] init];
[self enqueueBatchOfHTTPRequestOperations:operations
progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(#"Finished %d of %d", numberOfFinishedOperations, totalNumberOfOperations);
}
completionBlock:^(NSArray *operations) {
[[CoreDataManager sharedManager] persistFetchedStories:_sharedJSONManagerInsance.fetchedStories];
_sharedJSONManagerInsance.operations = nil;
NSLog(#"All operations finished");
}];
}
- (AFHTTPRequestOperation *)fetchJSON:(NSString*)requestUrl {
NSURL* jsonUrl = [NSURL URLWithString:requestUrl];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:jsonUrl];
AFJSONRequestOperation *operation = nil;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if([requestUrl isEqualToString:sourceUrl1]) {
NSArray* arr = [[JSON valueForKeyPath:#"data"] valueForKey:#"children"];
for (NSDictionary *item in arr) {
FetchedStory* fs = [[FetchedStory alloc] init];
fs.title = [[item valueForKey:#"data"]valueForKey:#"title"];
fs.url = [[item valueForKey:#"data"]valueForKey:#"url"];
fs.score = [[item valueForKey:#"data"]valueForKey:#"score"];
fs.source = #"source1";
[self.fetchedStories addObject:fs];
}
}
else if([requestUrl isEqualToString:sourceUrl2]) {
NSArray* arr = [JSON valueForKeyPath:#"items"];
for (NSDictionary *item in arr) {
FetchedStory* fs = [[FetchedStory alloc] init];
fs.title = [item valueForKey:#"title"];
fs.url = [item valueForKey:#"url"];
NSString* scoreString = [item valueForKey:#"score"];
if(scoreString != nil && [scoreString length]!=0) {
NSRange spaceRange = [scoreString rangeOfString:#" "];
scoreString = [scoreString substringToIndex:spaceRange.location];
fs.score = [NSDecimalNumber decimalNumberWithString:scoreString];
fs.source = #"source2";
[self.fetchedStories addObject:fs];
}
}
}
} failure:nil];
return operation;
}
The crash happens before "All operations finished" logs to the console. Again, this only happens some of the time.
It looks like this is a bug with AFJSONRequestOperation's responseJSON method. I added a nil check and that seems to be a good bandaid.
Actually you forget to set HTTP Method Parameter it should be like this:
[request setHTTPMethod:#"get"]; // post, patch ....
Related
The feed in question is https://fritchcoc.podbean.com/feed/
I have this in my code to parse, but every time I run it, I get an error message of the following. I have tried to add exception breakpoints, but it is not showing me the line of code causing all the commotion. I have spent two hours on this with no luck at all figuring out the issue. I set every NSString to nil within the commands, just in case one of the item valueForChild was coming up with errors, but even with all nils, they had issues.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RSSEntry initWithBlogTitle:articleTitle:articleUrl:articleDate:articleImage:contentEncoded:]: unrecognized selector sent to instance
- (void)viewDidLoad {
[super viewDidLoad];
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feeds = [NSArray arrayWithObjects:#"https://fritchcoc.podbean.com/feed/",
nil];
[self refresh];
}
- (void)refresh {
for (NSString *feed in _feeds) {
NSURL *url = [NSURL URLWithString:feed];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[_queue addOperation:request];
}
}
- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
if ([rootElement.name compare:#"rss"] == NSOrderedSame) {
[self parseRss:rootElement entries:entries];
} else if ([rootElement.name compare:#"feed"] == NSOrderedSame) {
[self parseAtom:rootElement entries:entries];
} else {
NSLog(#"Unsupported root element: %#", rootElement.name);
}
}
- (void)requestFinished:(ASIHTTPRequest *)request {
[_queue addOperationWithBlock:^{
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
options:0 error:&error];
if (doc == nil) {
NSLog(#"Failed to parse %#", request.url);
} else {
NSMutableArray *entries = [NSMutableArray array];
[self parseFeed:doc.rootElement entries:entries];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//int newCounter = 0;
for (RSSEntry *entry in entries) {
// newCounter++;
int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
RSSEntry *entry1 = (RSSEntry *) a;
RSSEntry *entry2 = (RSSEntry *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:entry atIndex:insertIdx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
withRowAnimation:nil];
/* if (newCounter > 999) {
break;
}*/
}
}];
}
}];
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(#"Error: %#", error);
[self refresh];
}
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
NSLog(#"Go");
NSArray *channels = [rootElement elementsForName:#"channel"];
for (GDataXMLElement *channel in channels) {
NSString *blogTitle = [channel valueForChild:#"title"];
NSArray *items = [channel elementsForName:#"item"];
for (GDataXMLElement *item in items) {
NSString *articleTitle = [item valueForChild:#"title"];
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSString *theCategory = [item valueForChild:#"category"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
NSString *articleUrl = [[[[item elementsForName: #"enclosure"] lastObject] attributeForName: #"url"] stringValue];
NSString *picture = [[[[item elementsForName: #"media:content"] lastObject] attributeForName: #"href"] stringValue];
RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate
articleImage:picture
contentEncoded:nil
category:theCategory] autorelease];
if ([theCategory isEqualToString:#"Sermon"]) {
[entries addObject:entry];
}
}
}
}
In your code you have this call:
RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate
articleImage:picture
contentEncoded:nil
category:theCategory] autorelease];
It has a "category" last parameter, but your error message doesn't have it:
Terminating app due to uncaught exception
'NSInvalidArgumentException',
reason: '-[RSSEntry initWithBlogTitle:articleTitle:articleUrl:articleDate:articleImage:contentEncoded:]:
unrecognized selector sent to instance
Is it possible that you have made a new version of RSSEntry that has "category", but didn't recompile properly some of your parsing code, and that code is still trying to call an old method (without "category")?
If so, just do a Product - "Clean" - "Build", and pay attention to compilation errors an warnings.
I've noticed that you still are not using ARC (calling "autorelease"), which indicates to me that it's a really old code :)
I have a client that runs their search functionality on their website through cloudsearch. I have been going through the documentation for days, and haven't been able to make a successful search request. I created an NSMutableRequest object, and am running that request through the AWSSignature method [signature interceptRequest:request]; but my task.result is coming back (null).
Here is my code:
AWSTask *task = [signature interceptRequest:request];
[task continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) {
NSLog(#"task.result fromSearch:%#", task.result);
NSData *responseData = task.result;
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"newStr:%#", newStr);
NSLog(#"task.error:%#", task.error);
return nil;
}];
Am I on the right track, or is there a better way to do this through the aws iOS sdk?
To put a little more flesh on the bones of Robert's comment, I did it with some help from AFNetworking like so:
#import <AWSCore/AWSSignature.h>
#import <AWSCore/AWSService.h>
#import <AWSCore/AWSCategory.h>
#import <AWSCore/AWSCredentialsProvider.h>
#import <AWSCore/AWSTask.h>
#import "AFNetworking.h"
- (void)viewDidLoad {
[super viewDidLoad];
self.queue = [[NSOperationQueue alloc] init];
}
- (void)performSearch {
AWSAnonymousCredentialsProvider* credentialsProvider = [[AWSAnonymousCredentialsProvider alloc] init];
NSString* searchHost = #"<CloudSearchEndPoint>.eu-west-1.cloudsearch.amazonaws.com";
NSString* query = [self.searchTerms aws_stringWithURLEncoding];
NSURL* searchURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://%#/2013-01-01/search?q=%#", searchHost, query]];
AWSEndpoint* endpoint = [[AWSEndpoint alloc] initWithURL:searchURL];
AWSSignatureV4Signer* signer = [[AWSSignatureV4Signer alloc] initWithCredentialsProvider:credentialsProvider endpoint:endpoint];
NSMutableURLRequest* mutableRequest = [[NSMutableURLRequest alloc] initWithURL:searchURL];
AWSTask* task = [signer interceptRequest:mutableRequest];
[task continueWithBlock:^id(AWSTask* _Nonnull t) {
if (t.error) {
NSLog(#"Error: %#", t.error);
} else if (t.completed) {
NSLog(#"Result is %#", t.result);
}
AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:mutableRequest success:^(NSURLRequest* request, NSHTTPURLResponse* response, id JSON) {
NSLog(#"Success fetching results!");
if (JSON) {
NSDictionary* hitsContainer = [JSON objectForKey:#"hits"];
NSArray* hits = [hitsContainer objectForKey:#"hit"];
NSMutableArray* allResults = [[NSMutableArray alloc] initWithCapacity:hits.count];
for (NSDictionary* hit in hits) {
NSDictionary* fields = [hit objectForKey:#"fields"];
[allResults addObject:fields];
}
self.searchResults = allResults;
[self.tableView reloadData];
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Failure fetching search results :-( %#", error);
}
];
[self.queue addOperation:operation];
return nil;
}];
I have a class where I request information from a provider class, in which after finalizing the job (asynchronous httpRequest block) needs to invoke a method [- (void) updateCountries] in the requester class. If I am not wrong this code worked in iOS 7, but now in iOS 8 it does not.
Can you please help me to understand why?
Methods in requester class:
- (void) viewWillAppear:(BOOL)animated {
//get countries to pickerView
webAPI = [[WebAPI alloc] init];
[webAPI retrieveCountries:self];
}
- (void) updateCountries {
//update countries content for pickerView
locationDAO = [[LocationDAO alloc] init];
countriesArray = [locationDAO getCountries];
[pickerView reloadAllComponents];
}
Lines in method in provider class where error happens:
SEL updateCountries = sel_registerName("updateCountries:");
[requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];
If you need to checkout the entire method in the provider class, here it is:
- (void) retrieveCountries:(id)requester {
// NSLog(#"engine report: firing retrieveCountries http get");
NSString *urlAsString = kRetrieveCountriesListAPI;
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:#"GET"];
[urlRequest setValue:#"application/json" forHTTPHeaderField:#"Content-type"];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"engine report: retrieveCountries server response: %#", response);
NSArray *level0 = [[NSArray alloc] initWithObjects:[NSJSONSerialization JSONObjectWithData:[[NSData alloc] initWithData:data] options:kNilOptions error:&error], nil];
NSArray *level1 = [level0 objectAtIndex:0];
LocationDAO *locationDAO = [[LocationDAO alloc] init];
[locationDAO deleteAllFromCountries];
for (int i = 0; i < [level1 count]; i++) {
CountryVO *countryVO = [[CountryVO alloc] init];
countryVO.myID = [[[level1 objectAtIndex:i] objectForKey:#"id"] integerValue];
countryVO.name = [[level1 objectAtIndex:i] objectForKey:#"country_name"];
[locationDAO saveCountryToDatabase:countryVO];
}
SEL updateCountries = sel_registerName("updateCountries:");
[requester performSelectorOnMainThread:updateCountries withObject:nil waitUntilDone:YES];
dispatch_async(dispatch_get_main_queue(), ^(void){
});
} else if ([data length] == 0 && error == nil){
NSLog(#"Nothing was downloaded.");
} else if (error != nil) {
NSLog(#"Error happened = %#", error);
} }];
}
THANK YOU A WHOLE LOT
Remove the : from the selector specification:
SEL updateCountries = sel_registerName("updateCountries");
Your method updateCountries doesn't take any arguments. So, when creating the selector, you should only write updateCountries (instead of updateCountries: which would indicate that this method takes an argument).
The reason why your app crashes is that when you try to perform this selector, the internals of your app are looking for a method called updateCountries on requester that takes one argument. This method doesn't exist, which is why the app crashes.
I'm trying to get Title data from the server but unfortunately I'm getting nothing as it is shown in the NSLog below. Shouldn't I get the Title dictionary? Please where would be my issue?
While I want to set that data in the UITableVeiw
- (void)getNews{
NSURL *url = [NSURL URLWithString:#"http://www.example.ashx"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
if (data.length > 0 && connectionError == nil)
{
NSDictionary *getData = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
if([[[getData objectForKey:#"Success"] stringValue] isEqualToString:#"1"]){
[dataNewsArray addObjectsFromArray:[[greeting objectForKey:#"Response"] objectForKey:#"Datasource"]];
}
NSDictionary *aDict = [NSDictionary dictionaryWithObject:dataHaberlerArray forKey:#"Title"];
NSLog(#"Check %#", aDict);
}
}];
}
NSLog result as like this;
Check {
Title = (
{
Content = "";
Date = "13.10.2014";
Time = "01:17:34";
Title = "example";
},
NSLog for dataNewsArray
2014-10-13 13:40:14.828 new_8[8742:346345] Check (
{
Content = " ";
Date = "13.10.2014";
Time = "01:38:53";
Title = "*test*";
},
write the below code to get one title
[[[aDict objectForKey:#"Title"] objectAtIndex:0] objectForKey:#"Title"]];
for all the titles use the below code
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
for (int i=0; i<[[aDict objectForKey:#"Title"] count]; i++) {
[titleArray addObject:[[[aDict objectForKey:#"Title"] objectAtIndex:i] objectForKey:#"Title"]];
}
The above solution provide the answer, but there is some other way using predicate to get the data
I'm trying to create a simple rss reader. The code works okay, except the UI hangs when the feeds are being updated. I thought I cobbled together the code to get the feed and parse it on a background queue while updating the UI on the mainQueue, but the table hangs pretty badly. Code below:
-(void)refreshFeed2
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
for (NSString *feed in _feeds) {
// iterate over all feeds
NSLog(#"feed=%#", feed);
NSURL *url = [NSURL URLWithString:feed];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
(void)[conn initWithRequest:request delegate:self];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([data length] == 0 && error == nil) {
// handle empty response
} else if (error != nil) {
// handle error
NSLog(#"Error %#", [error localizedDescription]);
} else if ([httpResponse statusCode] == 200) {
// data present and no errors
[queue addOperationWithBlock:^{
// parse feed on queue
RXMLElement *rss = [RXMLElement elementFromXMLData:data];
RXMLElement *rssChild = [rss child:#"channel"];
RXMLElement* title = [rssChild child:#"title"];
NSArray* items = [[rss child:#"channel"] children:#"item"];
NSMutableArray* result=[NSMutableArray array];
for (RXMLElement *e in items) {
// iterate over the articles
RSSArticle* article = [[RSSArticle alloc] init];
article.sourceTitle = [title text];
article.articleTitle = [[e child:#"title"] text];
article.articleDescription = [[e child:#"description"] text];
article.articleUrl = [NSURL URLWithString: [[e child:#"link"] text]];
NSString *articleDateString = [[e child:#"pubDate"] text];
article.articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
if (article.articleUrl != NULL) {
[result addObject:article];
}
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// update table on mainQueue
for (RSSArticle *article in result) {
// iterate over articles
int insertIdx = [_allEntries indexForInsertingObject:article sortedUsingBlock:^(id a, id b) {
RSSArticle *entry1 = (RSSArticle *) a;
RSSArticle *entry2 = (RSSArticle *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:article atIndex:insertIdx];
[self.LeftTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
}
}];
}];
}
}];
// Stop refresh control
[refreshControl endRefreshing];
}
}
Code that calls refreshFeed2:
- (void)viewDidLoad {
[super viewDidLoad];
self.allEntries = [NSMutableArray array];
self.feeds = [NSArray arrayWithObjects:
#"http://feeds.washingtonpost.com/rss/politics",
#"http://rss.cnn.com/rss/cnn_allpolitics.rss",
#"http://www.npr.org/rss/rss.php?id=1012",
#"http://www.slatedigital.com/support/feeds/rss_kb.php?s=fd5aa35e773dc3177b85a2126583f002",
nil];
}
//add refresh control to the table view
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self
action:#selector(refreshInvoked:forState:)
forControlEvents:UIControlEventValueChanged];
NSString* fetchMessage = [NSString stringWithFormat:#"Fetching Articles"];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:fetchMessage
attributes:#{NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:11.0]}];
[self.LeftTableView addSubview: refreshControl];
[self refreshInvoked:self forState:UIControlStateNormal];
}
-(void) refreshInvoked:(id)sender forState:(UIControlState)state {
NSOperationQueue *refreshQueue = [[NSOperationQueue alloc] init];
[refreshQueue addOperationWithBlock:^{
[self refreshFeed2];
}];
}
Any help?
Thanks!
Can you try this? replace
[self refreshInvoked:self forState:UIControlStateNormal];
by
[self performSelectorOnBackground:#selector(refreshFeed2) withObject:nil];
and replace the same instead of
-(void) refreshInvoked:(id)sender forState:(UIControlState)state {
[self performSelectorOnBackground:#selector(refreshFeed2) withObject:nil ];
}