iOS UITableView won't show anything when loading data from Dropbox - ios

I have a UITableView thats supposed to show the contents of a folder from dropbox when it loads. I know its getting the data from dropbox by using log statements. I reload the data in the table after it gets the data from drop box but it still shows nothing. Please help
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
uploadFileButton = [[UIBarButtonItem alloc] initWithTitle:#"Upload" style:UIBarButtonItemStyleBordered target:self action:#selector(uploadFile:)];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem = uploadFileButton;
self.title = #"DropBox";
[[self restClient] loadMetadata:#"/"];
}
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
for (DBMetadata *file in metadata.contents) {
NSLog(#"\t%#", file.filename);
[dropBoxArray addObject:file.filename];
}
NSLog(#"%#", dropBoxArray);
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [dropBoxArray objectAtIndex:indexPath.row];
NSLog(#"%#", cell.textLabel.text);
return cell;
}

You just need to show ProcessIndicator while you are making request
- (void)viewDidLoad
{
[super viewDidLoad];
uploadFileButton = [[UIBarButtonItem alloc] initWithTitle:#"Upload" style:UIBarButtonItemStyleBordered target:self action:#selector(uploadFile:)];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem = uploadFileButton;
self.title = #"DropBox";
[[self restClient] loadMetadata:#"/"];
[self showProcessIndicator]; //Your code for showing ProcessIndicator
}
And Hide when u get this method called.
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
for (DBMetadata *file in metadata.contents) {
NSLog(#"\t%#", file.filename);
[dropBoxArray addObject:file.filename];
}
NSLog(#"%#", dropBoxArray);
[self.tableView reloadData];
[self hideProcessIndicator];
}
This will definitely solve your problem.
Have a happy Coding ;)

Never mind i got it. This was the correct call to load metadata:
[[self restClient] loadMetadata:#""];

Related

Parsing JSON image urls in iOS error

I'm trying to parse a JSON file containing some links to images and some titles and times.
This is my code:
#import "PicturesViewController.h"
#import "DemoViewController.h"
#import "SecondViewController.h"
#import "AppDelegate.h"
#import "RNBlurModalView.h"
#import "PictureJSON.h"
#import "HMSegmentedControl.h"
#interface PicturesViewController ()
{
NSInteger refreshIndex;
NSArray *images;
}
#end
#implementation PicturesViewController
- (void)viewDidLoad
{
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:#[#"Instagram", #"Hashtags", #"Facebook"]];
[segmentedControl setFrame:CGRectMake(10, 10, 300, 60)];
[segmentedControl addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleBordered target:self action:#selector(showMenu)];
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler:)];
[self.view addGestureRecognizer:gestureRecognizer];
[self issueLoadRequest];
}
- (void)swipeHandler:(UIPanGestureRecognizer *)sender
{
[[self sideMenu] showFromPanGesture:sender];
}
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl1 {
[self issueLoadRequest];
}
- (void)segmentedControlSelectedIndexChanged:(id)sender
{
[self issueLoadRequest];
}
#pragma mark -
#pragma mark Button actions
- (void)showMenu
{
[[self sideMenu] show];
}
#pragma mark - Table view data source
- (void)issueLoadRequest
{
// Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://my-site/pictureparse.php?name=Name"]];
[self performSelectorOnMainThread:#selector(receiveData:) withObject:data waitUntilDone:YES];
});
}
- (void)receiveData:(NSData *)data {
// When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
// going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tweets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"PictureJSON";
PictureJSON *cell = (PictureJSON *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"PictureJSON" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
NSLog(#"%#", [cell class]);
cell.instaImage = [tweet objectForKey:#"link"];
cell.titleLabel.text = [tweet objectForKey:#"title"];
cell.timeLabel.text = [tweet objectForKey:#"published"];
return cell;
}
But when I launch my app I get this error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<PicturesViewController 0x758bf70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key instaImage.'
My JSON-file looks like this:
[
{
"link": "http://link.com/picture.jpg",
"title": "title",
"published": "0:12 PM 21/10"
},
{
"link": "http://link.com/picture.jpg",
"title": "title",
"published": "0:09 AM 21/10"
}
]
What am I doing wrong?
I think you have to check the property instaImage. I think you are accessing the property that you have not defined in nib.
Fist try to debug this . From the crash log I think you are doing wrong with the custom cell . first check your connection of the PictureJSON . and check your file owner it should be your custom cell. You can confirm this using breakpoint . if you get error while cell creation then above is the solution .
I think the problem is not with your JSON serialization, from your log this crash because of your static NSString *simpleTableIdentifier = #"PictureJSON";
Make sure, you have assigned your custom cell with this identifier !

Add edit functionality in contacts in iOS using Addressbook

I'm trying to create a simple create, display and edit contacts in Xcode 4.2 for iOS using AddressBook, so far I have done the create and display function. Now I need to implement edit function along with display. Now when I click on display it displays all the contacts and when I click on any name it shows info with a back button and a cancel button. I need to change the cancel button to edit and call the edit functionality. Below is the code of ViewController.m which I have done so far. Appreciate if u could give me a solution.
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
enum MainMenuChoice
{
menuDisplayContacts,
menuCreateContacts
};
#implementation ViewController
#synthesize menuArray;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Menu" ofType:#"plist"];
NSLog(#"%#",plistPath);
self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}
- (void)viewDidUnload {
self.menuArray = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [menuArray count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (aCell == nil) {
// Make the rows look like buttons
aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
aCell.textLabel.textAlignment = UITextAlignmentCenter;
}
aCell.textLabel.text = [[menuArray objectAtIndex:indexPath.section] valueForKey:#"title"];
return aCell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case menuDisplayContacts:
[self showContacts];
break;
case menuCreateContacts:
[self createContacts];
break;
default:
[self showContacts];
break;
}
}
//Show list of all people in Contacts
- (void)showContacts {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
picker.peoplePickerDelegate = self;
NSArray *displayItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],[NSNumber numberWithInt:kABPersonEmailProperty], [NSNumber numberWithInt:kABPersonBirthdayProperty],nil];
picker.displayedProperties = displayItems;
[self presentModalViewController:picker animated:YES];
}
// Dismisses the people picker and shows the application when users tap Cancel.
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
//For creating new contact when user tap create contacts
-(void)createContacts {
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc]init];
picker.newPersonViewDelegate=self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentModalViewController:navigation animated:YES];
}
// Dismisses the new-person view controller when user tap cancel.
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person {
[self dismissModalViewControllerAnimated:YES];
}
#end
I can get the desired result if I code like this in the tableview didselectRowAtIndexPath property, but I want to implement and integrate this code inside my displayContacts method instead of tableview here. Because this method is for initial screen for display and create contacts. Is there any other way that I could get the indexPath of the current selected names once I am inside the display contacts???
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ABPersonViewController *DVC=[[ABPersonViewController alloc]init];
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL);
NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0;i<nPeople;i++) {
ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
NSString *name = [self.contactAdd objectAtIndex:indexPath.row],*name2;
if(ABRecordCopyValue(person, kABPersonFirstNameProperty) != NULL) {
name2 = [[NSString stringWithFormat:#"%#", ABRecordCopyValue(person, kABPersonFirstNameProperty)]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
name2=[name2 stringByAppendingString:#" "];
name2=[name2 stringByAppendingString:[[NSString stringWithFormat:#"%#", ABRecordCopyValue(person, kABPersonLastNameProperty)]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
if([name isEqualToString:name2])
{
DVC.displayedPerson=person;
DVC.allowsEditing=YES;
[self.navigationController pushViewController:DVC animated:YES];
break;
}
}
}
-(BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[peoplePicker dismissModalViewControllerAnimated:NO];
ABPersonViewController *picker = [[ABPersonViewController alloc] init];
picker.personViewDelegate = self;
picker.displayedPerson = person;
// Allow users to edit the person’s information
picker.allowsEditing = YES;
[self.navigationController pushViewController:picker animated:YES];
return YES;
}
Got the solution with help of this function, but now stuck in adding delete function :P

viewWillAppear is not executing

I have a tableview loaded withe records, see the code below:
- (void)viewDidLoad
{
self.navigationItem.title=#"Accounts";
// NSString *accountFile = [[NSBundle mainBundle] pathForResource:#"Accounts2" ofType:#"plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *plistPath = [documentPath stringByAppendingPathComponent:#"Accounts2.plist"];
accounts = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(#"Accounts contains : %#", accounts);
account = [accounts objectForKey:#"Account"];
NSLog(#"account %#", account);
number = [accounts objectForKey:#"Number"];
dueDay = [accounts objectForKey:#"DayDue"];
minAmount = [accounts objectForKey:#"MinAmount"];
balance = [accounts objectForKey:#"Balance"];
NSLog(#"data loaded");
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
// if (!_objects) {
// _objects = [[NSMutableArray alloc] init];
// }
// [_objects insertObject:[NSDate date] atIndex:0];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// [self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
if (!self.addView) {
self.addView = [[AddView alloc] initWithNibName:#"AddView" bundle:nil];
}
[self.navigationController pushViewController:self.addView animated:YES];
NSLog(#"I am done, now what");
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Number of records is %d", [account count]);
return [account count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
NSString *nameOfAccount = [account objectAtIndex:indexPath.row];
cell.textLabel.text = nameOfAccount;
NSString *accountNumber = [number objectAtIndex:indexPath.row];
cell.detailTextLabel.text = accountNumber;
NSLog(#"Index %d", indexPath.row);
return cell;
}
I want to add a record to table, so I use the "InsertNewObject" method ... new screen appears, I am able to add the record to my plist, and then I execute the following line in the DetailView to return:
[self.navigationController popToRootViewControllerAnimated:NO];
Now, I want to reload my TableView with my new record:
I thought I just had to have the following viewWillAppear method like below:
- (void) viewWillAppear {
NSLog(#"View will appear");
[super viewWillAppear:YES];
[self.tableView reloadData];
}
but, my NSLog is never executed, therefore my program is never getting here... am I doing something incorrectly, thank you for any response
I have added the following two methods:
- (void) navigationController:(UINavigationController *) navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
[self.tableView reloadData];
}
-(void) navigationController:(UINavigationController *) navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
[self.tableView reloadData];
}
and changed the line
[self.navigationController popToRootViewControllerAnimated:YES];
still not working, is something still wrong.
You are not using the correct method. You are leaving out the animated argument, try using -(void)viewWillAppear:(BOOL)animated:
See UIViewController - viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"View will appear");
[self.tableView reloadData];
[super viewWillAppear:animated];
}
if you are using a nav controller I belive the correct way to reload a view would be to call somethihng like this
-(void) viewWillAppear: (BOOL) animated
{
[self.tableView reloadData];
}
You could also create your own custom reloadData method that sets what you want changed and then call that method.
As pointed out, viewWillAppear takes an animated argument. However, even when you add that, popping a view controller off the navigation stack does not trigger a call to viewWillAppear:. You need to use UINavigationControllerDelegate's navigationController:willShowViewController:animated: and navigationController:didShowViewController:animated: methods to reload your table view.

didselectrowatindexpath not called

I am facing a problem where the didselectrowatindexpath is not getting called.
My tableview is set in viewdidload as follows:
- (void)viewDidLoad
{
//[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
self.detailViewController = (klViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
Now my cellForRowAtIndexPath gets called and I am reusing the cells as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"menuSelection1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AmbassadorInfoData * data = [self.LoadMenuItems objectAtIndex:indexPath.row];
cell.textLabel.text = data.treeName;
return cell;
}
I have checked other answers and mostly said that there could be the use of didDeselectRowAtIndexPath might be used. But this is not the case here.
Now my problem is in my split view controller whenever I select any option the corresponding detail view doesn't get displayed and is blank. None of my methods in the rootview controller (klViewController) gets called. What could be the reason for this?
Following are the all methods related to UITableView. It is implemented in the same class as in viewDidLoad
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.LoadMenuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"menuSelection1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AmbassadorInfoData * data = [self.LoadMenuItems objectAtIndex:indexPath.row];
cell.textLabel.text = data.treeName;
return cell;
}
-(NSArray *)LoadMenuItems
{
NSMutableArray * menuData = [[NSMutableArray alloc] initWithCapacity:5];
AmbassadorInfoData * data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Ambassador Info";
data.treeDescription = #"This is temp data.";
NSString * file = [[NSBundle mainBundle] pathForResource:#"oak" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"AmbassadorInternalList";
data.treeDescription = #"This is temp data.).";
file = [[NSBundle mainBundle] pathForResource:#"DouglasFir" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Text 3";
data.treeDescription = #"This is temp data.";
file = [[NSBundle mainBundle] pathForResource:#"SugarMaple" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Text 4";
data.treeDescription = #"This is temp data.";
file = [[NSBundle mainBundle] pathForResource:#"RedMaple" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
data=nil;
data = [[AmbassadorInfoData alloc] init];
data.treeName = #"Text 5";
data.treeDescription = #"This is temp data.";
file = [[NSBundle mainBundle] pathForResource:#"Pine" ofType:#"jpg"];
data.treePhoto = [UIImage imageWithContentsOfFile:file];
[menuData addObject:data];
menuItems = [[NSArray alloc] initWithArray:(NSArray *)menuData];
data=nil;
return menuItems;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AmbassadorInfoData * selectedItem =(AmbassadorInfoData *)[self.menuItems objectAtIndex:[self.tableView indexPathForSelectedRow].row];
detailObject = selectedItem;
[detailViewController setDetailItem:detailObject];
}
#end
It could be that your program is stuck in a long running operation because of the way you're using LoadMenuItems. Every time numberOfRowsInSection and cellForRowAtIndexPath is called, you're recreating the entire array -- this is bad. You should call LoadMenuItems once, in viewDidLoad perhaps, and once you've created that array, use it, not a call to LoadMenuItems when you need to get data out. I don't know if this is what is causing your problem, but you should definitely fix it, and see if it makes a difference.
Try these modifications:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"TableView methods are called! problem is getting AmbassadorInfoData object")
AmbassadorInfoData * selectedItem =(AmbassadorInfoData *)[self.menuItems objectAtIndex:indexPath.row];
if (!selectedItem) NSLog(#"Unable to get Ambassador object, check your menuItems");
[self.detailViewController setDetailItem:selectedItem]; //notice "self."
}
also, uncomment [super viewDidLoad];
EDIT:
it appears that you are using the code you don't understand, in this case i am suggesting to learn some basics....here's a great site that helped me understand things quite a few times
and this is a guide from Apple: Table View Programming Guide

EXC_BAD_ACCESS with NSXMLParser and ARC

Beginner in iOS development, I'll try to realize an application which displays an RSS feed by an XML file.
In the viewDidLoad of my UITableView class, I'm using an UIActivityIndicator to wait until the data is loading.
But, at the moment that the app will be back to the main thread, I've an EXC_BAC_ACCESS code 2 at the end of the parseXMLStart function. I don't understand why...
Here the error message:
Thread 6 : 0-[NSXMLParser dealloc]
Message : EXC_BAC_ACCESS (code=2, address=0xc)
Line : 0xbb0840: movl (%eax,%ecx), %ecx
I don't know what and where is my error. How can I fix it?
Here is my code:
=> Class Type :: UITableViewController
>> Header
#interface DataListViewController : UITableViewController {
UIActivityIndicatorView *activityView;
NSMutableArray *dataFromXML;
}
- (void)parseXMLStart;
- (void)parseXMLDone;
#end
>> Main
#implementation DataListViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = #"View 1";
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = self.view.center;
[self performSelectorInBackground:#selector(parseXMLStart) withObject:nil];
[activityView startAnimating];
[activityView setHidesWhenStopped:YES];
[self.view addSubview:activityView];
}
#pragma mark - UIActivityIndicator Methods
- (void)parseXMLStart
{
// To Show the animation
sleep(1);
dataFromXML = [[NSMutableArray alloc] init];
// COMMENT TO TEST /*
[dataFromXML addObject:#"Element 1"];
[dataFromXML addObject:#"Element 2"];
[dataFromXML addObject:#"Element 3"];
// */ COMMENT TO TEST
// ------------------------------------------------------------------------------------------------------------------------------------
// UNCOMMENT TO TEST
/*
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"words" ofType:#"xml"];
NSURL *url = [[NSURL alloc] initWithString:[[NSString stringWithFormat:#"file://%#",filePath] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
XML2ObjectParser *parserWords = [[XML2ObjectParser alloc] parseXMLAtURL:url toObject:#"Word" parseError:nil];
NSLog(#">> parserWords Items Count :: %i", parserWords.items.count);
for (int i = 0; i < [parserWords.items count]-1; i++) {
Word *aWord = [[Word alloc] init];
aWord = (Word *)[[parserWords items] objectAtIndex:i];
[dataFromXML addObject:aWord];
}
NSLog(#">> dataFromXML Count :: %i", dataFromXML.count);
*/
// UNCOMMENT TO TEST
// --------------------------------------------------------------------------------------------------------------------------------------------
// EXC_BAD_ACCESS (code=2, address=0xc)
// Thread 6 : 0-[NSXMLParser dealloc]
// 0xbad840: movl (%eax,%ecx), %ecx
// --------------------------------------------------------------------------------------------------------------------------------------------
[self performSelectorOnMainThread:#selector(parseXMLDone) withObject:nil waitUntilDone:YES];
}
- (void)parseXMLDone
{
[activityView stopAnimating];
[self.tableView reloadData];
}
# pragma mark - Table View Method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataFromXML count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [dataFromXML objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DataListDetailViewController *_dataListDetailViewController = [[DataListDetailViewController alloc] init];
_dataListDetailViewController.title = [dataFromXML objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:_dataListDetailViewController animated:YES];
}
#end
It looks to me like your selector is typed incorrectly. you have #selector(parsingXMLDone) and it should be #selector(parseXMLDone)
I've find the error.
It wasn't my mistake but the ARC.
I'm explain, when parsing XML it's finished, the background thread is killed and after ARC wants to dealloc the NSXMLParser.
That's why, it causes an EXC_BAD_ACCESS, Because ARC want to dealloc an already deallocated object (AKA NSXMLParser).
The solution was to compile my class without ARC, using the flag "fno-objc-arc" in the Build Phases of the Target.
See : NSXMLParser gives EXC_BAD_ACCESS only with ARC enabled
Thanks for the help.

Resources