I'm using NSXMLParser and I get a memory leak that points to NSConcreteMapTable, whatever that is:
The leak occurs at this line of code when calling the parser from my AppDelegate.m:
I have searched for a solution and can't see what I'm doing wrong.
Here is my code.
Any help is greatly appreciated.
lq
// * * * XMLParser.h * * *
#import <Foundation/Foundation.h>
#protocol NSXMLParserDelegate;
#interface XMLParser : NSObject
<NSXMLParserDelegate>
{
NSMutableArray *xmlArray;
BOOL storingCharacters;
float xmlDataVersion;
}
#property (nonatomic, retain) NSMutableArray *xmlArray;
#property (nonatomic) BOOL storingCharacters;
#property (nonatomic, assign) float xmlDataVersion;
-(BOOL)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error;
#end
// * * * XMLParser.m * * *
#import "XMLParser.h"
#implementation XMLParser
#synthesize xmlArray;
#synthesize storingCharacters;
#synthesize xmlDataVersion;
- (BOOL)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error {
BOOL result = YES;
if (xmlArray == nil) {
// this array holds row data extracted from the XML parser didStartElement method
xmlArray = [[NSMutableArray alloc] init];
}
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
if (parser != nil) {
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[[parser setShouldResolveExternalEntities:NO];
}
[parser parse];
if (parseError && error) {
*error = parseError;
result = NO;
}
[parser release];
return result;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if (qName) {
elementName = qName;
}
// Check the data version of the XML Data against my stored value
if ([elementName isEqualToString:#"data"]) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.xmlDataVersion = [[attributeDict objectForKey:#"version"] floatValue];
float storedDataVersion = [userDefaults floatForKey:kDataVersion];
if (self.xmlDataVersion <= storedDataVersion) {
// - - - - -> Abort parsing if the same or earlier data versions
[parser abortParsing];
}
}
if ([elementName isEqualToString:#"FirstColumnName"]) {
storingCharacters = YES;
} else if ([elementName isEqualToString:#"SecondColumnName"]) {
storingCharacters = YES;
// ... total of 16 elements
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (storingCharacters) {
[self.xmlArray addObject:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (qName) {
elementName = qName;
}
// - - - - -> If at the end of a data row, save changes to object model
if ([elementName isEqualToString:#"ROW"]) {
// - - - - -> Make sure the data has the required number of elements before taking any action
if ([self.xmlArray count] == 16) {
// … //Store or Update Data in SQLite store depending on data values
}
[self.xmlArray removeAllObjects];
}
storingCharacters = NO;
}
-(void)dealloc {
[xmlArray release];
[super dealloc];
}
// * * * AppDelegate.m * * *
#import "XMLParser.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSURL *xmlURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"FileName" ofType:#"xml"]];
NSError *parseError = nil;
XMLParser *xmlParse = [[XMLParser alloc] init];
[xmlParse parseXMLFileAtURL:xmlURL parseError:&parseError];
[xmlParse release];
. . .
}
I found a solution in another SO post:
Use:
NSData * dataXml = [[NSData alloc] initWithContentsOfURL:URL];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataXml];
[dataXml release];
Instead of:
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
The leak goes away.
This is probably a leak in Apple code, since Foundation is reported as "Responsible Library". There's probably nothing you can do, but to report the bug to apple.
Related
The problem is that I have error when I try to press magnifying glass (lens) and in index goes to C instead of C. The written code is as follows:
///
// RBook.h
// ObjBook
//
// Created by pan on 24/08/16.
// Copyright © 2016 pan. All rights reserved.
//
#import <Foundation/Foundation.h>
#interface RBook : NSObject
{
}
#property (nonatomic,assign) int ID;
#property (nonatomic,strong) NSString *ISBN;
#property (nonatomic,strong) NSString *Code;
#property (nonatomic,strong) NSString *RBMDate;
#property (nonatomic,strong) NSString *Category;
#property (nonatomic,strong) NSString *FTitle;
#property (nonatomic,strong) NSString *FSubTitle;
#property (nonatomic,strong) NSString *GTitle;
#property (nonatomic,strong) NSString *GSubTitle;
#property (nonatomic,strong) NSString *Authors;
#property (nonatomic,strong) NSString *Publisher;
#property (nonatomic,strong) NSString *Cover;
#property (nonatomic,strong) NSString *CovPhoto;
#property NSInteger section;
#end
//
// RBooks.m
// ObjBook
//
// Created by pan on 24/08/16.
// Copyright © 2016 pan. All rights reserved.
//
#import "RBook.h"
#implementation RBook
#synthesize ID;
#synthesize ISBN;
#synthesize Code;
#synthesize RBMDate;
#synthesize Category;
#synthesize FTitle;
#synthesize FSubTitle;
#synthesize GTitle;
#synthesize GSubTitle;
#synthesize Authors;
#synthesize Publisher;
#synthesize Cover;
#synthesize CovPhoto;
-(NSString *) getName
{
return [NSString stringWithFormat:#"%d %# %# %# %# %# %# %# %# %# %# %# %#",self.ID,self.ISBN,self.Code,self.RBMDate,self.Category,self.FTitle,self.FSubTitle,self.GTitle,self.GSubTitle,self.Authors,self.Publisher,self.Cover,self.CovPhoto];
}
#end
//
// RBooksTableViewController.h
// MyRBooks
//
// Created by pan on 30/08/16.
// Copyright © 2016 pan. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "FMDBDataAccess.h"
#interface RBooksTableViewController : UITableViewController<UISearchBarDelegate , UITableViewDataSource , UITableViewDelegate> {
NSString *tID;
NSString *tFTitle;
NSString *tGTitle;
NSString *tAuthors;
NSString *tPublisher;
NSString *found;
NSString *from;
}
#property (nonatomic,strong) NSMutableArray *books;
#property (nonatomic,strong) NSMutableArray *filteredbooks;
#property (weak, nonatomic) IBOutlet UISearchBar *mysearchBar;
#property (nonatomic, assign) bool isFiltered;
//-(void) populateBooks;
#end
#import "RBooksTableViewController.h"
#import "RBooksTableViewCell.h"
#import "FMDBDataAccess.h"
#import "BooksDetailsViewController.h"
#import "RBook.h"
#implementation RBooksTableViewController
#synthesize books;
#synthesize filteredbooks;
#synthesize mysearchBar;
#synthesize isFiltered;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
mysearchBar.delegate = (id)self;
tID = #"A/A :";
tFTitle = #"FOREIGN TITLE :";
tGTitle = #"GREEK TITLE :";
tAuthors = #"AUTHORS. :";
tPublisher = #"PUBLISHERS :";
// [self populateBooks];
self.books = [NSMutableArray arrayWithCapacity:1];
NSMutableArray *booksTemp;
// Get the books array from the database
self.books = [[NSMutableArray alloc] init];
FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
booksTemp = [db getBooks];
UILocalizedIndexedCollation *indexedCollation =
[UILocalizedIndexedCollation currentCollation];
// Iterate over the products, populating their section number
for (RBook *therbook in booksTemp) {
NSInteger section = [indexedCollation sectionForObject:therbook
collationStringSelector:#selector(FTitle)];
therbook.section = section;
// NSLog(#" section is %d" , section);
}
// Get the count of the number of sections
NSInteger sectionCount = [[indexedCollation sectionTitles] count];
// NSLog(#" sectionCount is %d" , sectionCount);
// Create an array to hold the sub arrays
NSMutableArray *sectionsArray = [NSMutableArray
arrayWithCapacity:sectionCount];
// Iterate over each section, creating each sub array
for (int i=0; i<=sectionCount; i++) {
NSMutableArray *singleSectionArray = [NSMutableArray
arrayWithCapacity:1];
[sectionsArray addObject:singleSectionArray];
}
// Iterate over the products putting each product into the correct sub-array
for (RBook *therbook in booksTemp) {
[(NSMutableArray *)[sectionsArray objectAtIndex:therbook.section]
addObject:therbook];
}
// Iterate over each section array to sort the items in the section
for (NSMutableArray *singleSectionArray in sectionsArray) {
// Use the UILocalizedIndexedCollation sortedArrayFromArray: method to
// sort each array
NSArray *sortedSection = [indexedCollation
sortedArrayFromArray:singleSectionArray
collationStringSelector:#selector(FTitle)];
[self.books addObject:sortedSection];
// NSLog(#" sortedCollection is %#" , sortedSection);
}
}
- (void)viewDidUnload
{
// [self setSearchBar:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.tableView)
{
return [books count];
}
return 1;
}
NSIndexPath *currentSelection;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
currentSelection = indexPath;
[self showDetailsForIndexPath:indexPath];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount;
if(self.isFiltered)
{
rowCount = filteredbooks.count;
} else
rowCount = [[books objectAtIndex:section] count];
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
RBooksTableViewCell *cell = (RBooksTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[RBooksTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
RBook* rbook;
if(isFiltered)
rbook = [filteredbooks objectAtIndex:[indexPath row]];
else
// rbook = [books objectAtIndex:[indexPath row]];
rbook = [[books objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
// cell.textLabel.font = [UIFont systemFontOfSize: 17.0]; εχουν μπει στο RBooksTableViewCell.m
// cell.imageView.bounds = CGRectMake(15,17,80,128); εχουν μπει στο RBooksTableViewCell.m
// [[cell textLabel] setText:[NSString stringWithFormat:#"%# %#\r %# %# ", tFirstName,customer.firstName,tLastName,customer.lastName]];
// cell.IDlabel!.text = "\(books.ID_Lbl)"
[[cell textLabel] setText:[NSString stringWithFormat:#"%# %d\r %# %#\r %# %#\r %# %#\r %# %#",tID,rbook.ID,tFTitle,rbook.FTitle ,tGTitle,rbook.GTitle,tAuthors,rbook.Authors,tPublisher,rbook.Publisher]];
[[cell imageView] setImage:[UIImage imageNamed:rbook.CovPhoto]];
return cell;
}
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// create our UIImages
UIImage * defaultBG = [UIImage imageNamed:#"wood1.jpg"];
UIImage * selectedBG = [UIImage imageNamed:#"marble2.jpg"];
// Create UIImageView
UIImageView * defaultBGView = [[UIImageView alloc] initWithImage:defaultBG];
UIImageView * selectedBGView = [[UIImageView alloc] initWithImage:selectedBG];
// set the UIImageView
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = defaultBGView;
// change the background for the selectedview
cell.selectedBackgroundView = selectedBGView;
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.tableView resignFirstResponder];
}
#pragma mark - Table view delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length==0)
{
isFiltered = FALSE;
}else
{
isFiltered = TRUE;
filteredbooks = [[NSMutableArray alloc] init];
for(RBook* rbook in books)
{
NSRange ftitleRange = [rbook.FTitle rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange gtitleRange = [rbook.GTitle rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange authorsRange = [rbook.Authors rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange publisherRange = [rbook.Publisher rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(ftitleRange.location != NSNotFound || gtitleRange.location != NSNotFound || authorsRange.location != NSNotFound || publisherRange.location != NSNotFound )
{
[filteredbooks addObject:rbook];
}
}
}
found = #"THEY FOUND FOR:";
from = #" / ";
self.title = [NSString stringWithFormat:#"%# %# %lu %# %lu",found , searchText ,(unsigned long)filteredbooks.count ,from , (unsigned long)books.count];
[self. tableView reloadData];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self showDetailsForIndexPath:indexPath];
}
//-(void) populateBooks
//{
// self.books = [[NSMutableArray alloc] init];
//
// FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
//
// self.books = [db getBooks];
//}
-(void) showDetailsForIndexPath:(NSIndexPath*)indexPath
{
[self.mysearchBar resignFirstResponder];
BooksDetailsViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"BooksDetailsViewController"];
RBook* rbook;
if(isFiltered)
{
rbook = [filteredbooks objectAtIndex:indexPath.row];
}
else
{
// rbook = [books objectAtIndex:indexPath.row];
rbook = [[books objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
}
vc.rbook = rbook;
[self.navigationController pushViewController:vc animated:true];
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
// Make sure that the section will contain some data
if ([[books objectAtIndex:section] count] > 0) {
// If it does, get the section title from the
// UILocalizedIndexedCollation object
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]
objectAtIndex:section];
}
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;
} else {
NSMutableArray *retval = [NSMutableArray arrayWithObject:UITableViewIndexSearch];
[retval addObjectsFromArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
return retval;
}
}
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation]
sectionForSectionIndexTitleAtIndex:index];
//}
}
#end
The problem is somewhere in RBooksTableViewController.m
I have a small test function and I am getting memory allocation issue and memory warning after an hour of running. The problem is from the call to
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error1];
Below is my code.
Any helps would be appreciated.
Here is my app stat after 1 hour running:
Low: 712Kb, High 275.4 MB, Durarion 1 hour 26 min
2015-01-31 14:38:11.811 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:16.836 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:24.573 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:29.850 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:34.779 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:40.058 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:44.922 testMem[5268:546116] Received memory warning.
MainView.m
#import "ViewController.h"
#import "global.h"
#import "ServiceSvc.h"
#import "ClsTickets.h"
#interface ViewController ()
{
NSTimer* g_BACKUPTIMER;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
g_BACKUPTIMER = [NSTimer scheduledTimerWithTimeInterval:.4 target:self selector:#selector(runBackupProcess) userInfo:nil repeats:YES];
}
-(void)runBackupProcess
{
if (stopTimer == 0)
{
[NSThread detachNewThreadSelector: #selector(runtest) toTarget:self withObject:nil];
}
else
{
}
}
- (void) runtest
{
#autoreleasepool {
#try
{
ServiceSvc_DoAdminUnitIpad *req2 = [[ServiceSvc_DoAdminUnitIpad alloc] init];
req2.UnitDesc = #"Unit 6";
req2.CustomerID = #"800014";
req2.MachineID = nil;
req2.UnitID = #"1";
req2.GPSData = nil;
ServiceSynchronous* synCrhous = [[ServiceSynchronous alloc] init];
NSData* data = [synCrhous makeSynchrounousCall:req2];
data = nil;
req2 = nil;
}
#catch (NSException *exception)
{
}
#finally
{
}
} // end auto
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ServiceSvc.m
#implementation ServiceSynchronous
-(NSData*) makeSynchrounousCall:(ServiceSvc_DoAdminUnitIpad*) parameters
{
NSURLResponse* response = nil;
NSError* error1 = nil;
NSString *operationXMLString = #"<?xml version=\"1.0\"?> <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ServiceSvc=\"https://xxx/service.asmx\" xsl:version=\"1.0\"> <soap:Body> <ServiceSvc:DoAdminUnitIpad> <ServiceSvc:UnitID>1</ServiceSvc:UnitID> <ServiceSvc:UnitDesc>Unit 6</ServiceSvc:UnitDesc> <ServiceSvc:CustomerID>800014</ServiceSvc:CustomerID> </ServiceSvc:DoAdminUnitIpad> </soap:Body> </soap:Envelope>";
NSString *msgLength = [NSString stringWithFormat:#"%d",
[operationXMLString length]];
NSMutableURLRequest * req = [[NSMutableURLRequest alloc] init];
[req setURL:[NSURL URLWithString:#"https://xxx/service.asmx"]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://tempuri.org/webservice-name/method-name" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[operationXMLString dataUsingEncoding:NSUTF8StringEncoding]];
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error1];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:req];
if (error1 == nil)
{
}
else
{
}
operationXMLString = nil;
req = nil;
response = nil;
return data;
}
#end
#implementation ServiceSvc_DoAdminUnitIpad
- (id)init
{
if((self = [super init])) {
UnitID = 0;
UnitDesc = 0;
CustomerID = 0;
MachineID = 0;
GPSData = 0;
}
return self;
}
- (NSString *)nsPrefix
{
return #"ServiceSvc";
}
- (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix
{
}
/* elements */
#synthesize UnitID;
#synthesize UnitDesc;
#synthesize CustomerID;
#synthesize MachineID;
#synthesize GPSData;
/* attributes */
- (NSDictionary *)attributes
{
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
return attributes;
}
+ (ServiceSvc_DoAdminUnitIpad *)deserializeNode:(xmlNodePtr)cur
{
ServiceSvc_DoAdminUnitIpad *newObject = [ServiceSvc_DoAdminUnitIpad new];
[newObject deserializeAttributesFromNode:cur];
[newObject deserializeElementsFromNode:cur];
return newObject;
}
- (void)deserializeAttributesFromNode:(xmlNodePtr)cur
{
}
- (void)deserializeElementsFromNode:(xmlNodePtr)cur
{
}
#end
ServiceSvc.h
#interface ServiceSvc_DoAdminUnitIpad : NSObject {
/* elements */
NSString * UnitID;
NSString * UnitDesc;
NSString * CustomerID;
NSString * MachineID;
NSString * GPSData;
/* attributes */
}
- (NSString *)nsPrefix;
- (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix;
- (void)addAttributesToNode:(xmlNodePtr)node;
- (void)addElementsToNode:(xmlNodePtr)node;
+ (ServiceSvc_DoAdminUnitIpad *)deserializeNode:(xmlNodePtr)cur;
- (void)deserializeAttributesFromNode:(xmlNodePtr)cur;
- (void)deserializeElementsFromNode:(xmlNodePtr)cur;
/* elements */
#property (retain) NSString * UnitID;
#property (retain) NSString * UnitDesc;
#property (retain) NSString * CustomerID;
#property (retain) NSString * MachineID;
#property (retain) NSString * GPSData;
/* attributes */
- (NSDictionary *)attributes;
#end
#interface ServiceSynchronous : NSObject {
}
- (NSData*) makeSynchrounousCall:(ServiceSvc_DoAdminUnitIpad*) parameters;
#end
After some research, I came to the conclusion that Apple's NSURLConnection is the issue. The problem was reported back in 2008 and Apple had acknowledged it but now it is 2015 and the problem is still there. I have switched to asynchronous call and the memory allocation issue is still there, but smaller. Those geeks at Apple are a joke.
I met a problem with NSXMLParser. My source is run well in iOS7/XCode 5, but crashing in iOS8.1/XCode 6. Crashing error is:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSXMLParser does not support reentrant parsing.'
I tried with other solution in this post, but error still happened. Anyone can help me more?
My source like this
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate, NSXMLParserDelegate>
{
NSXMLParser *xmlParser_;
...
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self parseData];
....
}
- (void)parseData
{
titleList_ = [[NSMutableArray alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:fDetail ofType:fXML];
if (filePath)
{
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
if (myText)
{
countPage_ = 2;
NSData *xmlData = [myText dataUsingEncoding:NSUTF16StringEncoding];//NSUTF8StringEncoding];
xmlParser_ = [[NSXMLParser alloc] initWithData:xmlData];
xmlParser_.delegate = self;
[xmlParser_ parse];
}
}
}
The same code works fine for me in iOS 8/XCode 6.
Here is my code:
NSString *elementname;
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self parseData];
return YES;
}
- (void)parseData
{
titleList_ = [[NSMutableArray alloc] init];
NSString *fDetail = [NSString stringWithFormat:#"sample"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:fDetail ofType:#"xml"];
if (filePath)
{
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
if (myText)
{
countPage_ = 2;
NSData *xmlData = [myText dataUsingEncoding:NSUTF16StringEncoding];//NSUTF8StringEncoding];
xmlParser_ = [[NSXMLParser alloc] initWithData:xmlData];
xmlParser_.delegate = self;
[xmlParser_ parse];
}
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
elementname = elementName;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
elementname = elementName;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([elementname isEqualToString:#"CatalogId"])
{
int64_t cId = [string longLongValue];
NSLog(#"%lld",cId);
}
}
#end
Is there any good tutorial/source code to parse jSon data into the cocos2d project? I know how to parse jSon (also the XML) into the xcode and display into the tableview but I need to do that for my cocos2d project.
Here is what I was trying to do:
#import "Eighties.h"
#import "HelloWorldLayer.h"
#import "GameScene.h"
#import "JSON.h"
#define kLatestKivaLoansURL #"http://api.kivaws.org/v1/loans/search.json?status=fundraising"
#implementation Eighties
#synthesize responseData;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
Eighties *layer = [Eighties node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *bg = [CCSprite spriteWithFile:#"bg.jpg"];
[bg setPosition:ccp(winSize.width/2, winSize.height/2)];
[self addChild:bg z:0];
/*
CCMenuItem *menuItems = [CCMenuItemImage itemWithNormalImage:#"back_pink.png" selectedImage:#"back_blue.png" block:^(id sender) {
NSLog(#"Pressed");
[[SimpleAudioEngine sharedEngine] playEffect:#"tongue-clap.wav"];
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[GameScene scene] withColor:ccWHITE]];
}];
*/
CCMenuItem *menuItems2 = [CCMenuItemImage itemWithNormalImage:#"back_pink.png" selectedImage:#"back_blue.png" target:self selector:#selector(loadData)];
menuItems2.position = ccp(winSize.width/2-50, winSize.height/2-50);
CCMenu *menu = [CCMenu menuWithItems:menuItems2, nil];
menu.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:menu];
}
return self;
}
-(void)test {
NSLog(#"Success");
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Test Method Called"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
-(void)loadData
{
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kLatestKivaLoansURL]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (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 {
[connection release];
self.responseData = nil;
}
#pragma mark -
#pragma mark Process loan data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:#"loans"];
[responseString release];
//choose a random loan
for (int i=0; i<=18; i++) {
NSDictionary* loan = [latestLoans objectAtIndex:i];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:#"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:#"loan_amount"];
//float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
//NSString* name = [loan objectForKey:#"name"];
//NSString* country = [(NSDictionary*)[loan objectForKey:#"location"] objectForKey:#"country"];
//set the text to the label
/*
label.text = [NSString stringWithFormat:#"Latest loan: %# from %# needs another $%.2f, please help",
name,country,outstandingAmount
];
*/
NSLog(#"%d",i);
//NSLog(#"%#",label.text);
NSLog(#"\n");
/*
UIAlertView *message = [[UIAlertView alloc] initWithTitle:name
message:country
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
*/
}
}
#end
There are a lot of ways to deserialize JSON objects, some ways are even baked into the SDK.
This question details a few ways you can approach the problem.
or you can take your JSON and have this utility generate the parsing code for you
https://itunes.apple.com/us/app/json-accelerator/id511324989?mt=12
I have a problem in getting a method to work, and I am totally confused.
I am unable to hand over a string as a variable for my method.
I call the function even with the string, no variable currently.
Engine *myEngine = [Engine sharedInstance];
[myEngine getContentArrayFromEngine:#"zumbra"];
My method
-(NSMutableArray*) getContentArrayFromEngine:(NSString *)catName{
NSMutableSet* categorieContent = [[NSMutableSet alloc] init];
NSLog(#"Catname:%#", catName);
//some more code
}
NSLOG output
2011-12-18 18:49:44.165 Zitate[77224:15203] Catname:(null)
Why is catName empty ???
edit1: the complete code
ThirdViewController.m
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString* myTempCatname;
myTempCatname = cell.textLabel.text;
// NSLog(#"test select %#", myTempCatname);
DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"detailzitat"];
[self.navigationController pushViewController:detailVC animated:YES];
Engine *myEngine = [Engine sharedInstance];
[myEngine getContentArrayFromEngine:myTempCatname];
}
and in the engine.m
-(NSMutableArray*) getContentArrayFromEngine:(NSString *)catName{
NSMutableSet* categorieContent = [[NSMutableSet alloc] init];
NSLog(#"Übergebener Catname:%#", catName);
// catName=#"zumbra";
// NSLog(#"Inhalt InhalteFromWeb:%#", InhalteFromWeb);
NSLog(#"Catname:%#", catName);
unsigned count = [InhalteFromWeb count];
while (count--) {
NSLog(#"count %d %#", count, [[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY]);
if([[[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY] isEqualToString:catName]) {
[categorieContent addObject:[InhalteFromWeb objectAtIndex:count]];
NSLog(#"Row %d has Content%#",count, [InhalteFromWeb objectAtIndex:count]);
}
}
NSLog(#"Inhalt Category:%#", categorieContent);
NSArray* tempAr = [[NSArray alloc] initWithArray:[categorieContent allObjects]];
return [NSMutableArray arrayWithArray:tempAr];
}
EDIT2:
Ok, even the hint with the catName did not work. so I have changed my code a little bit.
I have an array with a category, title, content, author, image for each row
I would like to do two things
1) get a unique list of all categories (its working fine)
2) when tapping on one of these categories , open a detailView , show the first element of this category, jump to previous and next item in category by swiping around.
For this, I am going to SET the category I have chosen
First try was to handover in the method, which did not work.
Now I though, SET the category in my engine.h and when displaying the single item, get the array for this category back.
but again, the value of the category is not stored.
ThirdViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface ThirdViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
NSMutableArray* CategoryList;
}
#property (nonatomic, retain) NSMutableArray* CategoryList;
#end
ThirdViewController.m
#import "ThirdViewController.h"
#import "engine.h"
#import "DetailViewController.h"
#implementation ThirdViewController
#synthesize CategoryList;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
Engine *myEngine = [Engine sharedInstance];
CategoryList = [myEngine getCategories];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [CategoryList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [CategoryList objectAtIndex:indexPath.row];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString* myTempCatname;
myTempCatname = cell.textLabel.text;
DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"detailzitat"];
[self.navigationController pushViewController:detailVC animated:YES];
Engine *myEngine = [Engine sharedInstance];
[myEngine setCategName:myTempCatname];
NSLog(#"Aufruf %#", myTempCatname);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#import "engine.h"
#interface DetailViewController : UIViewController<MFMailComposeViewControllerDelegate> {
IBOutlet UILabel *authorLabel;
IBOutlet UILabel *categoryLabel;
IBOutlet UILabel *titleLabel;
IBOutlet UITextView *contentTextView;
NSString *authorText, *contentText, *categoryText, *titleText, *imageText, *catName;
NSMutableArray *contentArray;
}
#property (nonatomic, retain) IBOutlet UITextView *contentTextView;
#property (nonatomic, retain) IBOutlet UILabel *authorLabel;
#property (nonatomic, retain) IBOutlet UILabel *categoryLabel;
#property (nonatomic, retain) IBOutlet UILabel *titleLabel;
#property (nonatomic, retain) NSString *authorText, *contentText, *categoryText, *titleText, *imageText, *catName;
#property (nonatomic, retain) NSMutableArray *contentArray;
-(IBAction)vorher:(id)sender;
-(IBAction)nachher:(id)sender;
#end
DetailViewController.m
#import "DetailViewController.h"
#implementation DetailViewController
#synthesize contentTextView;
#synthesize authorText, contentText, categoryText, titleText, imageText;
#synthesize authorLabel, categoryLabel, titleLabel;
#synthesize contentArray;
#synthesize catName;
int contentIndex;
int contentMax;
- (IBAction)swipeDetected:(UIGestureRecognizer *)sender {
NSLog(#"Right Swipe detected");
}
-(IBAction) vorher:(id)sender {
NSLog(#"-----VORHER Button gedrückt-------");
if (contentIndex==0) {contentIndex=contentMax-1;}
else {contentIndex--;}
titleText = [[contentArray objectAtIndex:contentIndex] objectForKey:TITLE];
authorText= [[contentArray objectAtIndex:contentIndex] objectForKey:AUTHOR];
contentText= [[contentArray objectAtIndex:contentIndex] objectForKey:CONTENT];
authorLabel.text=authorText;
titleLabel.text=titleText;
contentTextView.text=contentText;
}
-(IBAction) nachher:(id)sender {
NSLog(#"-----Nachher Button gedrückt-------");
if (contentIndex==contentMax-1) {contentIndex=0;}
else {contentIndex++;}
titleText = [[contentArray objectAtIndex:contentIndex] objectForKey:TITLE];
authorText= [[contentArray objectAtIndex:contentIndex] objectForKey:AUTHOR];
contentText= [[contentArray objectAtIndex:contentIndex] objectForKey:CONTENT];
authorLabel.text=authorText;
titleLabel.text=titleText;
contentTextView.text=contentText;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
Engine *myEngine = [Engine sharedInstance];
contentArray = [myEngine getContentArrayFromEngine];
contentMax = [contentArray count];
UISwipeGestureRecognizer *swipeRecognizerRight =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(vorher:)];
swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizerRight];
UISwipeGestureRecognizer *swipeRecognizerLeft =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(nachher:)];
swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizerLeft];
titleText = [[contentArray objectAtIndex:contentIndex] objectForKey:TITLE];
authorText= [[contentArray objectAtIndex:contentIndex] objectForKey:AUTHOR];
contentText= [[contentArray objectAtIndex:contentIndex] objectForKey:CONTENT];
authorLabel.text=authorText;
titleLabel.text=titleText;
contentTextView.text=contentText;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
engine.h
//#import
#define AUTHOR #"author"
#define CATEGORY #"cat"
#define CONTENT #"content"
#define IMAGE #"image"
#define TITLE #"title"
#interface Engine : NSObject {
NSMutableArray* InhalteFromWeb;
NSInteger maxAnzahlInhalte;
NSString* categNameStorage;
}
+ (Engine *) sharedInstance;
- (NSMutableArray*) getZitateArrayFromEngine;
- (NSInteger) getMaxAnzahlZitateFromEngine;
- (NSString*) getAutor:(NSInteger)pos;
- (NSString*) getZitat:(NSInteger)pos;
- (NSString*) getAuthor:(NSInteger)pos;
- (NSString*) getCategory:(NSInteger)pos;
- (NSString*) getContent:(NSInteger)pos;
- (NSString*) getImage:(NSInteger)pos;
- (NSString*) getTitle:(NSInteger)pos;
-(NSMutableArray*) getContentArrayFromEngine;
-(void) setCategName:(NSString *) categNameVariable;
-(NSString*) getCategName;
-(NSMutableArray*) getCategories;
#end
engine.m
#import "Engine.h"
#implementation Engine
static Engine *_sharedInstance;
- (id) init
{
if (self = [super init])
{
// custom initialization
//Beginn my code
NSURL *url = [NSURL URLWithString:#"http://www.*/iMotivate.plist"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
InhalteFromWeb = [[NSMutableArray alloc] initWithContentsOfURL:url];
maxAnzahlInhalte = [InhalteFromWeb count];
}
else
{
NSLog(#"Connection failed");
}
}
return self;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// NSLog(#"Recieving Response...");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// NSLog(#"Recieving Data...");
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message : #"An error has occured.Please verify your internet connection."
delegate:nil
cancelButtonTitle :#"OK"
otherButtonTitles :nil];
[alert show];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// NSLog(#"DONE. Received Quotes: %d", maxAnzahlZitate);
}
// ###########
+ (Engine *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[Engine alloc] init];
}
return _sharedInstance;
}
// Getter and Setter for WebArray
- (NSMutableArray*) getZitateArrayFromEngine{
return InhalteFromWeb;
}
- (NSInteger) getMaxAnzahlZitateFromEngine{
return maxAnzahlInhalte;
}
- (NSString*) getAutor:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:AUTHOR];
}
- (NSString*) getZitat:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:CONTENT];
}
// #######
- (NSString*) getAuthor:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:AUTHOR];
}
- (NSString*) getCategory:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:CATEGORY];
}
- (NSString*) getContent:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:CONTENT];
}
- (NSString*) getImage:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:IMAGE];
}
- (NSString*) getTitle:(NSInteger)pos{
return [[InhalteFromWeb objectAtIndex:pos] objectForKey:TITLE];
}
-(NSArray*) getCategories {
NSMutableSet* categorieSet = [[NSMutableSet alloc] init];
unsigned count = [InhalteFromWeb count];
while (count--) {
NSString *tempString;
tempString=[[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY];
// NSLog(#"tempString %#", tempString );
[categorieSet addObject:tempString];
}
// NSLog(#"categories from engine %#", categorieSet);
NSArray* tempAr = [[[NSArray alloc] initWithArray:[categorieSet allObjects]]sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
return [NSMutableArray arrayWithArray:tempAr];
}
-(void) setCategName:(NSString *) categNameVariable
{ NSLog(#"categNameStorage 2%#",categNameStorage);
categNameStorage=categNameVariable;
NSLog(#"setCategName 1 %#",categNameVariable);
NSLog(#"categNameStorage 2%#",categNameStorage);
}
-(NSString*) getCategName {
return categNameStorage;
}
-(NSMutableArray*) getContentArrayFromEngine{
NSMutableSet* categorieContent = [[NSMutableSet alloc] init];
NSLog(#"Übergebener Catname:%#", categNameStorage);
// NSLog(#"Inhalt InhalteFromWeb:%#", InhalteFromWeb);
unsigned count = [InhalteFromWeb count];
while (count--) {
// NSLog(#"count %d %#", count, [[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY]);
if([[[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY] isEqualToString:categNameStorage]) {
[categorieContent addObject:[InhalteFromWeb objectAtIndex:count]];
// NSLog(#"Row %d has Content%#",count, [InhalteFromWeb objectAtIndex:count]);
}
}
// NSLog(#"Inhalt Category:%#", categorieContent);
NSArray* tempAr = [[NSArray alloc] initWithArray:[categorieContent allObjects]];
return [NSMutableArray arrayWithArray:tempAr];
}
#end
There has to be a variable conflict with 'catName' in your view controller. I'm not sure why the view controller would be in scope, but I bet if you change your parameter to "inCatName" it will be fine. It's good to use naming conventions like that for this reason.