Core data do not fetch record - iphone

i added website field in core data after adding other attributes to xcdatamodel, and i am saving this attribute like this
- (void)fetchandSaveGWTGsSince:(NSString *)since
{
NSString *completeURL = [NSString stringWithFormat:#"%#api.php?action=get_all_gwtgs&sinceDate=%#", BASE_URL, since];
NSURL *url = [NSURL URLWithString:completeURL];
// self.loggingEnable = NO;
if (self.loggingEnable == YES)
{
NSLog(#"%#", [NSString stringWithFormat:#"%i", [since integerValue]-18000]);
NSLog(#"%#", since);
NSLog(#"%#", [[NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[since integerValue]] description]);
NSLog(#"%#", [[NSDate dateWithTimeIntervalSince1970:((NSTimeInterval)[since integerValue])-18000] description]);
NSLog(#"%#", completeURL);
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.timeoutInterval = REQUEST_TIMEOUT;
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:#"text/html"]];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSDictionary *resDict = (NSDictionary *)JSON;
if (self.loggingEnable == YES)
{
NSLog(#"Response GWTGs : %#", resDict);
}
if ([[resDict objectForKey:#"status"] isEqualToString:#"success"])
{
NSArray *data = [[resDict objectForKey:#"data"] objectAtIndex:0];
NSManagedObjectContext *context = [[Common apd] managedObjectContext];
if ([data count] > 0)
{
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSDictionary *gwtgDict = (NSDictionary *)obj;
if ([gwtgDict objectForKey:#"gwtg_id"])
{
NSArray *locationInfos = [self getGWTGsWithGWTG_ID:[gwtgDict objectForKey:#"gwtg_id"]];
if ([locationInfos count] > 0)
{
for (Location *location in locationInfos)
{
[context deleteObject:location];
NSError *error = nil;
if (![context save:&error]) {
// Handle the error.
if (self.loggingEnable == YES)
{
NSLog(#"error deleting gwtg : %#", [location description]);
}
}
}
}
}
}];
}
[data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSDictionary *gwtgInfo = (NSDictionary *)obj;
/*
gwtg_status = 0 => Inactive Record
gwtg_status = 1 => New Record/Active Record
gwtg_status = 2 => Updated Record
gwtg_status = 3 => Deleted Record
*/
if ([[gwtgInfo objectForKey:#"gwtg_status"] intValue] != 3)
{
GWTG *gwtg = [NSEntityDescription insertNewObjectForEntityForName:#"GWTG" inManagedObjectContext:context];
gwtg.gwtg_id = ([[gwtgInfo objectForKey:#"gwtg_id"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"gwtg_id"];
NSData *celData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"celebrity_name"]];
NSString *celName = [[NSString alloc] initWithData:celData encoding:NSStringEncodingConversionAllowLossy];
gwtg.celebrity_name = ([celName isKindOfClass:[NSNull class]]) ? #"" : celName;
gwtg.location_id = ([[gwtgInfo objectForKey:#"location_id"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"location_id"];
NSData *loc_cityData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"location_city"]];
NSString *loc_cityName = [[NSString alloc] initWithData:loc_cityData encoding:NSStringEncodingConversionAllowLossy];
gwtg.location_city = ([loc_cityName isKindOfClass:[NSNull class]]) ? #"" : loc_cityName;
NSData *cityData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"city"]];
NSString *cityName = [[NSString alloc] initWithData:cityData encoding:NSStringEncodingConversionAllowLossy];
gwtg.city = ([cityName isKindOfClass:[NSNull class]]) ? #"" : cityName;
NSData *stateData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"state"]];
NSString *stateName = [[NSString alloc] initWithData:stateData encoding:NSStringEncodingConversionAllowLossy];
gwtg.state = ([stateName isKindOfClass:[NSNull class]]) ? #"" : stateName;
NSData *countryData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"country"]];
NSString *countryName = [[NSString alloc] initWithData:countryData encoding:NSStringEncodingConversionAllowLossy];
gwtg.country = ([countryName isKindOfClass:[NSNull class]]) ? #"" : countryName;
gwtg.a_consuming = ([[gwtgInfo objectForKey:#"a_consuming"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"a_consuming"];
gwtg.a_playing = ([[gwtgInfo objectForKey:#"a_playing"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"a_playing"];
gwtg.a_lodging = ([[gwtgInfo objectForKey:#"a_lodging"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"a_lodging"];
gwtg.a_shopping = ([[gwtgInfo objectForKey:#"a_shopping"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"a_shopping"];
gwtg.a_recommending = ([[gwtgInfo objectForKey:#"a_recommending"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"a_recommending"];
gwtg.w_g_here = ([[gwtgInfo objectForKey:#"w_g_here"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"w_g_here"];
NSData *locData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"location_name"]];
NSString *locName = [[NSString alloc] initWithData:locData encoding:NSStringEncodingConversionAllowLossy];
gwtg.location_name = ([locName isKindOfClass:[NSNull class]]) ? #"" : locName;
//location_address, we_go_here, credit
NSData *locAdData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"location_address"]];
NSString *locAdName = [[NSString alloc] initWithData:locAdData encoding:NSStringEncodingConversionAllowLossy];
gwtg.location_address = ([locAdName isKindOfClass:[NSNull class]]) ? #"" : locAdName;
gwtg.location_phone = ([[gwtgInfo objectForKey:#"location_phone"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"location_phone"];
gwtg.location_zip_code = ([[gwtgInfo objectForKey:#"location_zip_code"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"location_zip_code"];
NSData *wghData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"we_go_here"]];
NSString *wghName = [[NSString alloc] initWithData:wghData encoding:NSStringEncodingConversionAllowLossy];
gwtg.we_go_here = ([wghName isKindOfClass:[NSNull class]]) ? #"" : wghName;
gwtg.wgh_date = ([[gwtgInfo objectForKey:#"wgh_date"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"wgh_date"];
NSData *creData = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"credit"]];
NSString *creName = [[NSString alloc] initWithData:creData encoding:NSStringEncodingConversionAllowLossy];
gwtg.credit = ([creName isKindOfClass:[NSNull class]]) ? #"" : creName;
NSData *webSite_Data = [NSData dataWithBase64EncodedString:[gwtgInfo objectForKey:#"website"]];
NSString *add_website = [[NSString alloc] initWithData:webSite_Data encoding:NSStringEncodingConversionAllowLossy];
gwtg.website = ([add_website isKindOfClass:[NSNull class]]) ? #"" : add_website;
gwtg.gwtg_status = ([[gwtgInfo objectForKey:#"gwtg_status"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"gwtg_status"];
gwtg.gwtg_isactive = ([[gwtgInfo objectForKey:#"gwtg_isactive"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"gwtg_isactive"];
gwtg.addeddate = ([[gwtgInfo objectForKey:#"addeddate"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"addeddate"];
gwtg.modifieddate = ([[gwtgInfo objectForKey:#"modifieddate"] isKindOfClass:[NSNull class]]) ? #"" : [gwtgInfo objectForKey:#"modifieddate"];
if (self.loggingEnable == YES)
{
NSLog(#"----Location----");
NSLog(#"%#", gwtg.gwtg_id);
NSLog(#"%#", gwtg.celebrity_name);
NSLog(#"%#", gwtg.location_id);
NSLog(#"%#", gwtg.city);
NSLog(#"%#", gwtg.state);
NSLog(#"%#", gwtg.country);
NSLog(#"%#", gwtg.a_consuming);
NSLog(#"%#", gwtg.a_playing);
NSLog(#"%#", gwtg.a_lodging);
NSLog(#"%#", gwtg.a_shopping);
NSLog(#"%#", gwtg.a_recommending);
NSLog(#"%#", gwtg.w_g_here);
NSLog(#"%#", gwtg.location_name);
NSLog(#"%#", gwtg.location_address);
NSLog(#"%#", gwtg.location_phone);
NSLog(#"%#", gwtg.location_zip_code);
NSLog(#"%#", gwtg.we_go_here);
NSLog(#"%#", gwtg.wgh_date);
NSLog(#"%#", gwtg.credit);
NSLog(#"%#", gwtg.website);
NSLog(#"%#", gwtg.gwtg_status);
NSLog(#"%#", gwtg.addeddate);
NSLog(#"%#", gwtg.modifieddate);
NSLog(#"%#", gwtg.gwtg_isactive);
}
NSError *error;
if (![context save:&error]) {
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
}
}];
if ([[NSUserDefaults standardUserDefaults] objectForKey:LAST_TIME_GWTG_UPDATED])
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:LAST_TIME_GWTG_UPDATED];
}
NSDate *date = [NSDate date];
NSLog(#"date last time : %#", [data description]);
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:#"%.f", [date timeIntervalSince1970]] forKey:LAST_TIME_GWTG_UPDATED];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRequestFinished object:nil userInfo:nil];
}
else
{
// [Common showAlertWithTitle:#"Warning" message:[resDict objectForKey:#"message"]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:LAST_TIME_GWTG_UPDATED])
{
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:#"0"] forKey:LAST_TIME_GWTG_UPDATED];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
if (self.loggingEnable == YES)
{
NSLog(#"error %#", [error description]);
}
[Common showAlertWithTitle:#"Error Retrieving" message:[NSString stringWithFormat:#"%#",[error localizedDescription]]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:LAST_TIME_GWTG_UPDATED])
{
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:#"0"] forKey:LAST_TIME_GWTG_UPDATED];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}];
[operation start];
}
and i am getting website url like this, but it always come empty,
NSString *urlString = [NSString stringWithFormat:#"%#", _gwtgDO.website];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
why it is always empty although i am getting website link from server correctly? is this due to reason that i added attribute later? what should i do now?

You are passing NSStringEncodingConversionOptions instead of NSStringEncoding. I suppose you want NSString *add_website = [[NSString alloc] initWithData:webSite_Data encoding:NSUTF8StringEncoding]; for UTF-8 encoding, for example.
More about the encodings here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

Related

CoreData inserting very huge DB exc_bad_access

I have ~60000 values in my DB.
I must to keep them all. But while inserting values I get exc_bad_access.
Here is my code:
if (context == nil)
{
context = [(radioAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(#"After managedObjectContext: %#", context);
}
[self performSelectorOnMainThread:#selector(deleteAllFromDB) withObject:nil waitUntilDone:YES];
// here is JSON parsing
int i=0;
#synchronized(self)
{
NSLog(#"Start");
NSEntityDescription *entity = [[NSEntityDescription entityForName:#"Station" inManagedObjectContext:context] autorelease];
for (NSString*string in stations)
{
if (![string isEqualToString:#""])
{
Station*station = [[[Station alloc] initWithEntity:entity insertIntoManagedObjectContext:nil] retain];
NSError *err = nil;
id parsedData = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers error:&err];
if (err) {
NSLog(#"error is %#", [err localizedDescription]);
}
[station setName:[parsedData objectForKey:#"nm"]];
[station setBit: [parsedData objectForKey:#"btr"]];
[station setEnabled: [NSNumber numberWithInt:1]];
//encoding id of the station
scanner = [NSScanner scannerWithString:[parsedData objectForKey:#"id"]];
[scanner scanHexInt:&tempInt];
NSNumber *numb = [[NSNumber alloc]initWithInt:i];
[station setNumber: [NSNumber numberWithInt:[numb intValue]]];
//encoding country ID
numb = [NSNumber numberWithInt:i];
[station setCountryID: [NSNumber numberWithInt:[numb intValue]]];
//encoding genre ID
numb = [NSNumber numberWithInt:i];
[station setGenerID:[NSNumber numberWithInt:[numb intValue]]];
[station setOrder:[NSNumber numberWithInt:i]];
[context insertObject:station];
float k = [stations count];
k = (i + 1)/k;
[self performSelectorOnMainThread:#selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
i++;
[scanner release];
[numb release];
[station release];
[parsedData release];
}
}
[context save:nil];
[entity release];
NSLog(#"Stop");
NSEntityDescription *entityGen = [NSEntityDescription entityForName:#"Genre" inManagedObjectContext:context];
NSURL* urlForGenre = [NSURL URLWithString:#"xxx.xxx"];
NSData *genres = [[NSData alloc] initWithContentsOfURL:urlForGenre];
NSError *err = nil;
NSArray *parsedGenres = [NSJSONSerialization JSONObjectWithData:genres options:NSJSONReadingMutableContainers error:&err];
if (err)
{
NSLog(#"error is %#", [err localizedDescription]);
}
for (int i =0; i<parsedGenres.count; i++)
{
Genre*gen = [[Genre alloc] initWithEntity:entityGen insertIntoManagedObjectContext:nil];
gen.name = [[parsedGenres objectAtIndex:i] objectForKey:#"nm"];
int tempInt;
NSScanner *scanner= [[NSScanner alloc] init];
scanner = [NSScanner scannerWithString:[[parsedGenres objectAtIndex:i] objectForKey:#"id"]];
[scanner scanInt:&tempInt];
NSNumber *numb = [[NSNumber alloc] init];
numb = [NSNumber numberWithInt:tempInt];
gen.number = [NSNumber numberWithInt:[numb integerValue]];
float k = [parsedGenres count];
k = (i + 1)/k;
[self performSelectorOnMainThread:#selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
[context insertObject:gen];
[gen release];
}
[entityGen release];
[context save:nil];
NSEntityDescription *entityCon = [NSEntityDescription entityForName:#"Country" inManagedObjectContext:context];
NSURL* urlForCoun = [NSURL URLWithString:#"yyy.yyy"];
NSData *countr = [[NSData alloc] initWithContentsOfURL:urlForCoun];
err = nil;
NSArray *parsedCountr = [NSJSONSerialization JSONObjectWithData:countr options:NSJSONReadingMutableContainers error:&err];
if (err)
{
NSLog(#"error is %#", [err localizedDescription]);
}
NSMutableArray* temp = [[NSMutableArray alloc] init];
for (int i =0; i<parsedCountr.count; i++)
{
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
countr.name = [[parsedCountr objectAtIndex:i] objectForKey:#"nm"];
int tempInt;
NSScanner *scanner= [[NSScanner alloc] init];
scanner = [NSScanner scannerWithString:[[parsedCountr objectAtIndex:i] objectForKey:#"id"]];
[scanner scanInt:&tempInt];
NSNumber *numb = [[NSNumber alloc] init];
numb = [NSNumber numberWithInt:tempInt];
countr.number = [NSNumber numberWithInt:[numb integerValue]];
[temp addObject:countr];
float k = [parsedCountr count];
k = (i + 1)/k;
[self performSelectorOnMainThread:#selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
[context insertObject:countr];
[countr release];
}
[context save:nil];
If my app doesn't crash, yes, it happens not by every launch. It crashes on:
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
Please help! And sorry for my terrible English.
Update:
On:
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
Thread 7: EXC_BAD_ACCESS (code=1, address=0x3f800008)
Other error is exc_bad_access too. But with code=2 and on other thread.
Update 2:
I enabled zombies mode in scheme of my debug. Nothing happened.
Update 3:
I think I have problems with memory.
GuardMalloc: Failed to VM allocate 4128 bytes
GuardMalloc: Explicitly trapping into debugger!!!
You can't pass nil as managedObjectContext parameter to initWithEntity:insertIntoManagedObjectContext: method. Try changing this:
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:nil];
to this
// pass managed object context parameter
Country*countr = [countr initWithEntity:entityCon insertIntoManagedObjectContext:context];
Based on the documentation and my experience you can pass nil as managedObjectContext.
I was also getting an EXC_BAD_ACCESS and it came that the reason was that I was not retaining the managed object model(I was only creating a model to get the entity description and then it was released).
In particular, this is what I was doing:
In a test class, in the setUp method:
- (void)setUp
{
[super setUp];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *modelURL = [bundle URLForResource:#"TestDatamodel" withExtension:#"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
STAssertNotNil(model, nil);
_entity = [[model entitiesByName] objectForKey:NSStringFromClass([MyEntity class])];
STAssertNotNil(_entity, nil);
// ...
}
Then in a test method I was creating a managed object without a context like this:
NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:_entity insertIntoManagedObjectContext:nil];
And I was getting EXC_BAD_ACCESS here.
The solution was to add an ivar to the test class to keep a reference to the managed object model.

Using GDataXML to Parse Included XML File

This XML will be included in the app:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Directory</title>
<description>Directory of Members</description>
<language>en</language>
<item>
<LastName></LastName>
<FirstName></FirstName>
<Address></Address>
<Phone></Phone>
<Email></Email>
</item>
<item>
<LastName></LastName>
<FirstName></FirstName>
<Address></Address>
<Phone></Phone>
<Email></Email>
</item>
</channel>
</rss>
I would like to be able to parse the XML and store into an RSSEntry Class that I have done before with other XMLs. The issue is that the only other XMLs I have worked with have been located online, and I use ASIHttpRequest Classes as part of the method, and am unsure how to go about changing the code to make it fit. I currently use this code:
- (void)viewDidLoad {
[super viewDidLoad];
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feeds = [NSArray arrayWithObjects:#"http://316apps.com/LakesideNews/feed/",
nil];
NSLog(#"%#", self.feeds);
[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)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
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 *articleUrl = [item valueForChild:#"guid"];
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
NSString *articleImage = [item valueForChild:#"content:encoded"];
RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate
articleImage:articleImage] autorelease];
[entries addObject:entry];
}
}
}
- (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:^{
for (RSSEntry *entry in entries) {
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:UITableViewRowAnimationRight];
}
}];
}
}];
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(#"Error: %#", error);
[self refresh];
}
I know to change all the areas for Title to things like Family name, etc., I just don't know how to go about getting the GDataXML to begin parsing the nested XML.
Ray Wenderlich has a fantastic tutorial covering exactly what you are doing. I would start there. Here's the link.

how to capture the required values from a URL

I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.
The url's will look like:
http://gmail.com?access_token=ab8w4azq2xv3dr4ab37vvzmh&token_type=bearer&expires_in=3600
How can I capture the value of the access_token which in this example is ab8w4azq2xv3dr4ab37vvzmh?
This code should do it:
- (NSString *)extractToken:(NSURL *)URL
{
NSString *urlString = [URL absoluteString];
NSRange start = [urlString rangeOfString:#"access_token="];
if (start.location != NSNotFound)
{
NSString *token = [urlString substringFromIndex:start.location+start.length];
NSRange end = [token rangeOfString:#"&"];
if (end.location != NSNotFound)
{
//trim off other parameters
token = [token substringToIndex:end.location];
}
return token;
}
//not found
return nil;
}
Alternatively, here is a more general solution that will extract all the query parameters into a dictionary:
- (NSDictionary *)URLQueryParameters:(NSURL *)URL
{
NSString *queryString = [URL query];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *parameters = [queryString componentsSeparatedByString:#"&"];
for (NSString *parameter in parameters)
{
NSArray *parts = [parameter componentsSeparatedByString:#"="];
NSString *key = [[parts objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([parts count] > 1)
{
id value = [[parts objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[result setObject:value forKey:key];
}
}
return result;
}
Good Category for NSDictionary:
#import "NSDictionary+URL.h"
#implementation NSDictionary (URL)
+ (NSDictionary *)dictionaryWithUrlString:(NSString *)urlString{
NSRange urlRange = [urlString rangeOfString:#"?"];
if(urlRange.length>0){
urlString = [urlString substringFromIndex:urlRange.length+urlRange.location];
}
NSArray *pairsArray = [urlString componentsSeparatedByString:#"&"];
NSMutableDictionary *parametersDictionary = [[NSMutableDictionary alloc] initWithCapacity:[pairsArray count]];
for(NSString *pairString in pairsArray){
NSArray *valuesArray = [pairString componentsSeparatedByString:#"="];
if([valuesArray count]==2){
[parametersDictionary setValue:[valuesArray objectAtIndex:1] forKey:[valuesArray objectAtIndex:0]];
}
}
return [parametersDictionary autorelease];
}
#end
NSMutableDictionary *querycomponent = [[NSMutableDictionary alloc] init];
if (![query isEqualToString:#""]){
NSArray *queryArray = [query componentsSeparatedByString:#"&"];
for (NSString *subquery in queryArray){
NSArray *subqueryArray = [subquery componentsSeparatedByString:#"="];
NSString *key = [subqueryArray objectAtIndex:0];
NSString *val = [subqueryArray objectAtIndex:1];
[querycomponent setObject:val forKey:key];
}
NSLog(#"querycomponent %#",querycomponent);
}

Crash on [responseData setLength:0];

I am making a async request and I'm getting a EXC_BAD_ACCESS on [responseData setLength:0];
The Code is:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"Connection failed: %#", [error description]);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
luckyNumbers1 = [responseString JSONValue];
NSUserDefaults *information = [NSUserDefaults standardUserDefaults];
/*NSArray *luckyNumbers = [json objectWithString:responseString error:&error];*/
NSLog(#"luckyNumbers1: %#", luckyNumbers1);
NSLog(#"user Info array is: %#", luckyNumbers1);
// [information setObject:[NSString stringWithFormat:#"%#", (NSString *)[luckyNumbers1 objectForKey:#"session"]] forKey:#"sessionID"];
NSDictionary *array = (NSDictionary *)[luckyNumbers1 objectForKey:#"data"];
NSEnumerator *inner = [luckyNumbers1 objectEnumerator];
id value;
while((value = [inner nextObject])) {
NSLog(#"user Info array is: %#", value);
if ( [value isKindOfClass:[NSDictionary class]] ) {
[information setObject:[value objectForKey:#"id"] forKey:#"userID"];
NSLog(#"uid is: %#", [value objectForKey:#"id"]);
[information setObject:[NSString stringWithFormat:#"%#", (NSString *)[value objectForKey:#"points_all"]] forKey:#"pointString"];
[information setObject:[NSString stringWithFormat:#"%#", (NSString *)[[[value objectForKey:#"leaderboards"] objectForKey:#"all"] objectForKey:#"position"]] forKey:#"rankString"];
if ((NSNull *)[value objectForKey:#"display_name"] == [NSNull null]) {
[information setObject:#"No Display Name" forKey:#"displayNameString"];
} else {
[information setObject:[NSString stringWithFormat:#"%#", (NSString *)[value objectForKey:#"display_name"]] forKey:#"displayNameString"];
}
if ((NSNull *)[value objectForKey:#"level"] == [NSNull null]) {} else {
[information setObject:[NSString stringWithFormat:#"%#", (NSString *)[[[value objectForKey:#"level"] objectForKey:#"definition"] objectForKey:#"name"]] forKey:#"levelString"];
}
pointsTV = [[UILabel alloc] initWithFrame:CGRectMake(222, 294, 441, 22)];
pointsTV.text = [NSString stringWithFormat:#"Points: %#", [information stringForKey:#"pointString"]];
pointsTV.backgroundColor = [UIColor clearColor];
pointsTV.textColor = [UIColor whiteColor];
[TVWindow addSubview:pointsTV];
rankLabelTV = [[UILabel alloc] initWithFrame:CGRectMake(222, 269, 441, 22)];
rankLabelTV.backgroundColor = [UIColor clearColor];
rankLabelTV.textColor = [UIColor whiteColor];
rankLabelTV.text = [NSString stringWithFormat:#"Rank: %#", (NSString *)[[[array objectForKey:#"leaderboards"] objectForKey:#"all"] objectForKey:#"position"]];
[TVWindow addSubview:rankLabelTV];
levelNameLabelTV = [[UILabel alloc] initWithFrame:CGRectMake(222, 244, 441, 22)];
levelNameLabelTV.backgroundColor = [UIColor clearColor];
levelNameLabelTV.textColor = [UIColor whiteColor];
if ((NSNull *)[value objectForKey:#"level"] == [NSNull null]) {
levelNameLabelTV.text = #"No Level";
} else {
levelNameLabelTV.text = [NSString stringWithFormat:#"Level: %#", (NSString *)[[[array objectForKey:#"level"] objectForKey:#"definition"] objectForKey:#"name"]];
}
[TVWindow addSubview:levelNameLabelTV];
pointLabel.text = [information stringForKey:#"pointString"];
pointLabel.textAlignment = UITextAlignmentLeft;
displayNameLabelTV.text = [information stringForKey:#"displayNameString"];
displayNameLabel. text = [information stringForKey:#"displayNameString"];
rankLabel.text = [information stringForKey:#"rankString"];
levelLabel.text = [information stringForKey:#"levelString"];
NSURL *url1 = [NSURL URLWithString: [NSString stringWithFormat:#"%#", (NSString *)[array objectForKey:#"picture_url"]]];
NSData *urlData1 = [NSData dataWithContentsOfURL:url1];
UIImage *image1 = [UIImage imageWithData:urlData1];
profilePicture.image = image1;
profilePictureTV.image = image1;
[information setObject:[NSString stringWithFormat:#"%#", (NSString *)[luckyNumbers1 objectForKey:#"session"]] forKey:#"sessionID"];
}}
NSURLRequest *request2;
request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://%#/api/widgets/%#/%#/players/%#/rewards.json", [information stringForKey:#"host"], [information objectForKey:#"apiKey"], [information objectForKey:#"URL"], [information objectForKey:#"userID"]]]];
NSLog(#"Badges Request is: %#", request2);
NSURLConnection *connection2;
connection2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self startImmediately:YES];
NSURLResponse *resp3;
NSData *cData2 = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&resp3 error:nil];
NSString *cDataString2 = [[NSString alloc] initWithData:cData2 encoding:NSUTF8StringEncoding];
[self getUsersBadges: cDataString2];
[[NSURLConnection alloc] initWithRequest:request2 delegate:self];
// pass cDataString into the JSON parser and update points
[connection release];
}
Without seeing your initialization of reponseData, it’s hard to be sure, but I would guess that you’re either declaring it as [NSMutableData data] (which will get autoreleased) or release/autoreleaseing it before you get to -connection:didReceiveResponse:. The solution is to make sure it doesn’t get released early, either by calling retain on it or by initializing it with [[NSMutableData alloc] init]. In either case, you’ll need to release it once you’re done, though it looks as if you’re doing that already in your -connectionDidFinishLoading:, so no change needed there.

How can I check If I got null from json?

Here I got from JSON
[{"photo":null}]
and I use this code
NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:#"photo"]];
How can I check it if I want to use if() ??
edit
here is JSON Data
[{"photo":
[{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"}
,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"}
,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]}
]
and here is my JSON parser
NSError *theError = nil;
NSString *URL = [NSString stringWithFormat:#"http://www.yohyeh.com/apps/get_sub_detail.php?id=%#&menu=photo",g_id];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [string JSONValue];
Thank for help
I believe most JSON parsers represent null as [NSNull null].
Considering jsonDict points to that single element in the array, then the following should work:
if ([jsonDict objectForKey:#"photo"] == [NSNull null]) {
// it's null
}
Edit based on comment: so jsonDict, despite its name, is an array. In that case, rename jsonDict to jsonArray to avoid further confusion. Then, considering jsonArray points to an array similar to the example posted in the question:
NSArray *photos = [jsonArray valueForKey:#"photo"];
for (id photo in photos) {
if (photo == [NSNull null]) {
// photo is null
}
else {
// photo isn't null
}
}
Further edit based on OP’s modified question:
NSArray *jsonArray = [string JSONValue];
NSArray *photos = [jsonArray valueForKey:#"photo"];
for (id photo in photos) {
if (photo == [NSNull null]) {
// photo is null
}
else {
// photo isn't null. It's an array
NSArray *innerPhotos = photo;
…
}
}
Macros can be helpful if payload is complex JSON structure having possible values.
#define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }
and macro can be referenced like
SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:#"name"]);
There's no easy way of dealing with this, but the way I do it is to make a category on NSObject:
#interface NSObject (NotNull)
- (instancetype)notNull;
#end
Implemented like so:
#implementation NSObject (NotNull)
- (instancetype)notNull
{
return self;
}
#end
#implementation NSNull (NotNull)
- (instancetype)notNull
{
return nil;
}
#end
Then you can send notNull to any optional object in a JSON dict and you'll get nil
back if it's NSNull. Otherwise you get the original object. For example:
self.parentIdentifier = [dictionary[#"parent_id"] notNull];
try to check for [jPhoto count] or [NSNull null]
Hope so this helps.
-(NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json{
NSMutableDictionary* strongjson=[json mutableCopy];
for (NSString *ktr in json) {
NSObject *str=[json objectForKey:ktr];
if ([str isKindOfClass:[NSArray class]]) {
if (!(str==[NSNull null])) {
NSArray *temp = [json allKeysForObject:str];
str=[[self ArrayCheckforNull:(NSMutableArray*)str]mutableCopy];
NSString *key = [temp objectAtIndex:0];
[strongjson removeObjectForKey:key];
[strongjson setObject:str forKey:key];
}
else
{
NSArray *temp = [strongjson allKeysForObject:str];
NSString *key = [temp objectAtIndex:0];
[strongjson removeObjectForKey:key];
[strongjson setObject:#"-----" forKey:key];
}
}
else if ([str isKindOfClass:[NSDictionary class]]) {
if (!(str==[NSNull null])) {
str=[[self jsonCheckforNull:str]mutableCopy];
NSArray *temp = [strongjson allKeysForObject:str];
NSString *key = [temp objectAtIndex:0];
[strongjson removeObjectForKey:key];
[strongjson setObject:str forKey:key];
}
else
{
NSArray *temp = [strongjson allKeysForObject:str];
NSString *key = [temp objectAtIndex:0];
[strongjson removeObjectForKey:key];
[strongjson setObject:#"-----" forKey:key];
}
}
else {
if (str ==[NSNull null]) {
NSArray *temp = [strongjson allKeysForObject:str];
NSString *key = [temp objectAtIndex:0];
[strongjson removeObjectForKey:key];
[strongjson setObject:#"----" forKey:key];
}
}
}
return strongjson;
}
-(NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr{
NSObject *str;
NSMutableArray* strongArray=[[[NSMutableArray alloc]initWithArray:arr]mutableCopy];
for (str in arr)
{
if ([str isKindOfClass:[NSArray class]]) {
if (!(str==[NSNull null])) {
str=[[self ArrayCheckforNull:(NSMutableArray *)str]mutableCopy];
[strongArray removeObjectAtIndex:0];
[strongArray addObject:str];
}
else
{
[strongArray removeObject:str];
[strongArray addObject:#"----"];
}
}
else if ([str isKindOfClass:[NSDictionary class]]) {
if (!(str==[NSNull null])) {
str=[[self jsonCheckforNull:(NSMutableDictionary*)str]mutableCopy];
[strongArray removeObjectAtIndex:0];
[strongArray addObject:str];
}
else
{
[strongArray removeObject:str];
[strongArray addObject:#"----"];
}
}
else {
if (str ==[NSNull null]) {
[strongArray removeObject:str];
[strongArray addObject:#"----"];
}
}
}
return strongArray;
}