Improve Load Time of Sectioned UITableView - objective-c

I am displaying a UITableView modally, but it takes about two seconds for it to appear, below is the code that is holding up the transition.
ModalViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// get all songs from iTunes library
MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
// put the songs into an array
self.songsArray = [songQuery items];
// create a sectioned array where songs are sectioned by title
self.sectionedSongsArray = [self partitionObjects:self.songsArray collationStringSelector:#selector(title)];
}
- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for(int i = 0; i < sectionCount; i++)
{
[unsortedSections addObject:[NSMutableArray array]];
}
for (id object in array)
{
NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
for (NSMutableArray *section in unsortedSections)
{
[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
The above code works fine, but its slow to load the modal view first time, is there a better way to do this? Thanks.

Yeah: don’t do it in -viewDidLoad. A better place would be in the view controller’s -init or -initWithNibNamed:bundle: or whatever, and in the background. Example:
- (id)init
{
self = [super init];
if(self)
{
// ...
dispatch_async(dispatch_get_global_queue(DISPATCH_PRIORITY_DEFAULT, 0), ^{
// since it's not on the main thread, you need to create your own autorelease pool to prevent leaks
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
self.songsArray = [songQuery items];
self.sectionedSongsArray = [self partitionObjects:self.songsArray collationStringSelector:#selector(title)];
// UI calls have to be on the main thread, so we go back to that here
dispatch_async(dispatch_get_main_queue(), ^{
if([self isViewLoaded])
{
[self.tableView reloadData];
}
});
// this releases any objects that got autoreleased earlier in the block
[pool release];
});
}
return self;
}
Your -tableView:numberOfRowsInSection: method should of course now check whether sectionedSongsArray is non-nil and in that case return 0 (or 1 if you want to display a “loading” cell, which you probably should).

Related

How to understand where the crash is taking place from xcode

I'm new to xcode and I don't understand how I'm supposed to tell why the app is crashing from this report. When I click "Open in project" it takes me to a place in the code with little information to work on.
Picture of crash report
- (void)didTapOnJobTableView:(UIGestureRecognizer *)recognizer {
CGPoint tapLocation = [recognizer
locationInView:self.tableViewJobList];
NSIndexPath *indexPath = [self.tableViewJobList
indexPathForRowAtPoint:tapLocation];
if (indexPath && indexPath.row >= 0 && indexPath.row <
[self.dispatchTable count]) {
SDMobileAppDelegate *appDelegate = [SDMobileAppDelegate me];
Dispatch *d = [self.dispatchTable objectAtIndex:indexPath.row];
<<<<Last exception backtrace here
if (d != nil && ![d isCancelled]) {
JobListTableViewCell *cell = (JobListTableViewCell *)
[self.tableViewJobList cellForRowAtIndexPath:indexPath];
CGPoint tapLocalzied = [recognizer locationInView:cell];
if (CGRectContainsPoint([cell.labelPrts frame], tapLocalzied)) {
if ([d.prtsPckQty integerValue] > 0) {
dispatchForPartsPick = d;
NSMutableString *partsPickText = [NSMutableString
stringWithString:#""];
NSArray *list = [PartsPick getListFromDatabase:self.db
forDispatch:dispatchForPartsPick];
for (PartsPick *p in list) {
NSString *location = #"";
if (![Utilities isEmpty:p.binLoc]) {
location = [NSString stringWithFormat:#"[at %#]", p.binLoc];
}
[partsPickText appendFormat:#"%# %# %# %#\r\n", p.qty,
location, [p.prtNmbr stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]], p.dscrptn];
if (![Utilities isEmpty:p.notes]) {
[partsPickText appendFormat:#"%#\r\n", p.notes];
}
[partsPickText appendString:#"\r\n"];
}
[ShowTextViewController show:self request:SEGUE_PARTSPICK_PREVIEW
header:[NSString stringWithFormat:lStr(#"PARTSPICK_PREVIEW"),
dispatchForPartsPick.invNmbr] message:#"" defaultText:partsPickText
editable:NO autoCap:UITextAutocapitalizationTypeNone delegate:nil];
} else {
[Utilities showOkAlertWithTitle:lStr(#"PTA")
andMessage:lStr(#"PARTSPICK_PREVIEW_NONE") onComplete:nil];
}
} else {
if ([Utilities allowedToGoToJob: d]) {
appDelegate.selectedDispatch = [NSNumber
numberWithLong:indexPath.row];
self.tabBarController.selectedIndex = 1;
} else {
[Utilities showOkAlertWithTitle:#"Cannot Continue"
andMessage:#"Cannot select until previous PVR is completed."
onComplete:nil];
}
}
} else {
[Utilities showOkAlertWithTitle:lStr(#"PTA")
andMessage:lStr(#"JOB_CANCELLED_CANNOT_SELECT") onComplete:nil];
}
}
}
Here is the code where this the above is used, specifically the gesture recognizer part:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self refresh];
XLog(#"Job List View will appear");
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(didTapOnJobTableView:)];
[self.tableViewJobList addGestureRecognizer:tap];
// startup the ticker
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self selector:#selector(countdownTimer:) userInfo:nil
repeats:YES];
SDMobileAppDelegate *appDelegate = [SDMobileAppDelegate me];
NSTimeInterval diff = [[appDelegate.refreshTimer fireDate]
timeIntervalSinceNow];
if (diff > 60) {
self.buttonForceRefresh.enabled = YES;
self.buttonForceRefresh.titleLabel.enabled = YES;
}
[self setTimeCardStatus];
}
That has lots of information to work with; you just don't know what it means yet. Been there - it's frustrating at times...
See Ray Wenderlich's site and Apple's docs to get started debugging:
https://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1
https://www.raywenderlich.com/10505/my-app-crashed-now-what-part-2
"Unrecognized Selector" or "Does not respond to selector" means the object being called doesn't understand or can't find the method you're trying to call.
When that message is related to the user interface or interaction, it often means you forgot to hook up the IBOutlet or IBAction from the UI element to the code, or there's a typo in the names somewhere, or the method you're trying to call doesn't exist (or isn't publicly exposed) in the object being called.
Make sure you've hooked up your elements from interface builder to the code for the class, that the names are consistent, and that the method didTapOnJobTableViewactually exists in the class JobListViewController.

nsmutabledictionary returning null when accessed outside of class

I have searched around a bit and not found an answer to my question. I am a beginner to objective-c and I am currently experimenting around with dictionaries. I have a class called "SETestBank" that creates 3 dictionaries of images and then adds them to a large dictionary. I am doing it this way because down the line I may add more smaller dictionaries. I've created a specific "accessBank" method to pull objects out via other classes.
SETestBank.m
#import "SETestBank.h"
#implementation SETestBank
#synthesize mathBankA, mathBankB, mathBankC, mathTest;
- (id)init
{
[self createBankA];
[self createBankB];
[self createBankC];
[self createTest];
return 0;
}
- (NSMutableDictionary *)createBankA
{
mathBankA = [[NSMutableDictionary alloc] init];
for (int i=0; i < 11; i++) {
NSString *aKey = [NSString stringWithFormat:#"%da", i];
NSString* imageName = [NSString stringWithFormat:#"%da.png",i];
[mathBankA setObject:imageName forKey:aKey];
NSLog(#"%#", aKey);
}
return mathBankA;
}
//same occurs to generate bankB and bankC
- (NSMutableDictionary *)createTest
{
mathTest = [[NSMutableDictionary alloc] init];
[mathTest addEntriesFromDictionary:mathBankA];
[mathTest addEntriesFromDictionary:mathBankB];
[mathTest addEntriesFromDictionary:mathBankC];
return mathTest;
}
- (NSString *)accessBank:(NSString *)accessor
{
NSString *img = [mathTest objectForKey:accessor];
return img;
}
#end
now this code seems to work fine. The dictionaries are created and console logs display the correct keys and object filenames (the images are all in the project and properly named)
However, when I access it in my view controller and try to apply an image to a UIImageView I get nothing.
SEViewController:
- (void)viewDidLoad
{
SETestBank *mathTest = [[SETestBank alloc] init];
UIImage *img = [UIImage imageNamed:[mathTest accessBank:#"1a"]];
NSString *test = [mathTest accessBank:#"1a"];
NSLog(#"%#", test);
[imageView setImage:img];
[self.view addSubview:imageView];
[super viewDidLoad];
}
the log here simply returns null along with a "CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 1.000000" error which I gather is from trying to assign null to an image. I'm stumped on this one. If I hardcode the image to display "1a.png" rather than trying to access it by key it works fine but that is not what I'm trying to accomplish. imageView is connected to a UIImageView in storyboard and the view controller is set to use SEViewController as it's class.
Thanks for any help!
In the init method, you return a 0, which means nothing (nil)
- (id)init
{
[self createBankA];
[self createBankB];
[self createBankC];
[self createTest];
return 0;
}
Change this to
- (id)init
{
if (self = [super init]) {
[self createBankA];
[self createBankB];
[self createBankC];
[self createTest];
}
return self;
}
may solve your problem.
Your init in SETestBank is returning 0, so your mathTest object is nil when you do
SETestBank *mathTest = [[SETestBank alloc] init];

Populating an IKImageBrowserView

I am currently working on building up a view which shows icons, and text labels to the right of the icons. After some searching, I've decided that an IKImageBrowserView is most suitable. As such, I've gone ahead and created my IKImageBrowserView and set it's data source as follows:
// Setup image browser view
IKImageBrowserView *browser = [IKImageBrowserView new];
// Build our datasource delegate
MyDataStore *ds = [[MyDataStore alloc] init];
[browser setDelegate:ds];
[browser setDataSource:ds];
NSImage *image = // . . . File path
NSString *imageID = // . . . Filename
IKBBrowserItem *item = [[IKBBrowserItem alloc] initWithImage:image imageID:imageID];
[[ds images] addObject:item];
I've also created my data source, implementing the two required methods in the protocol:
import "MyDataStore.h"
#implementation MyDataStore
- (id) init {
if ([super init] != nil) {
images = [NSMutableArray new];
}
return self;
}
- (NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *) aBrowser {
return [images count];
}
- (id) imageBrowser:(IKImageBrowserView *) aBrowser itemAtIndex:(NSUInteger)index {
return [images objectAtIndex:index];
}
- (NSMutableArray *) images {
return images;
}
#end
However, when I try and add an item (as shown in the first block of code), nothing shows up in my IKImageBrowserView. I suspect I'm doing something glaringly incorrect with this view. Anyone know what that may be?

TTLauncherItem: change badge immediately (or: how to refresh TTLauncherView)

I have a TTLauncherView with some TTLauncherItems. These show badges, representing messages from the network. I set the badges in viewWillAppear:, so if I switch to another view and then return, the correct badges are shown. But I want to update the badges as soon a message comes in.
Calling setNeedsDisplay on TTLauncherView doesn't help?
How can I refresh the TTLauncherView?
in my MessageReceiver class I do this:
TTNavigator* navigator = [TTNavigator navigator];
[(OverviewController *)[navigator viewControllerForURL:#"tt://launcher"] reloadLauncherView] ;
My TTViewController-derived OverviewController
#implementation OverviewController
- (id)init {
if (self = [super init]) {
self.title = OverviewTitle;
}
return self;
}
- (void)dealloc {
[items release];
[overView release];
[super dealloc];
}
-(void)viewDidLoad
{
[super viewDidLoad];
overView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
overView.backgroundColor = [UIColor whiteColor];
overView.delegate = self;
overView.columnCount = 4;
items = [[NSMutableArray alloc] init];
for(int i = 1; i <= NumberOfBars; ++i){
NSString *barID = [NSString stringWithFormat:NameFormat, IDPrefix, i];
TTLauncherItem *item = [[[TTLauncherItem alloc] initWithTitle:barID
image:LogoPath
URL:[NSString stringWithFormat:#"tt://item/%d", i]
canDelete:NO] autorelease];
[barItems addObject: item];
}
overView.pages = [NSArray arrayWithObject:items];
[self.view addSubview:overView];
}
-(void)viewWillAppear:(BOOL)animated
{
for(int i = 0; i <[barItems count]; i++){
TTLauncherItem *item = [items objectAtIndex:i];
NSString *barID = [NSString stringWithFormat:NameFormat, IDPrefix, i+1];
P1LOrderDispatcher *dispatcher = [OrderDispatcher sharedInstance];
P1LBarInbox *barInbox = [dispatcher.barInboxMap objectForKey:barID];
item.badgeNumber = [[barInbox ordersWithState:OrderState_New]count];
}
[super viewWillAppear:animated];
}
- (void)launcherView:(TTLauncherView*)launcher didSelectItem:(TTLauncherItem*)item
{
TTDPRINT(#"%#", item);
TTNavigator *navigator = [TTNavigator navigator];
[navigator openURLAction:[TTURLAction actionWithURLPath:item.URL]];
}
-(void)reloadLauncherView
{
[overView setNeedsDisplay];//This doesn't work
}
#end
I register my Controller with the LauncherView at the AppDelegate. In my messaging class I call [appDelegate reloadLauncherView]; that again will call this
-(void)reloadLauncherView
{
[self viewWillAppear:NO ];
}
on the Controller that contains the LauncherView.
I was having a very similar problem today, (modifying a TTLauncherItem, and not seeing my changes directly) and was able to solve it by making a call to [myLauncherView layoutSubviews]; BEFORE I modified the TTLauncherItem. I actually tracked it down in the code, and this was because layoutSubviews will re-create the LauncherView's _buttons array (which is what needed to happen, in my case).

NSCFArray leak in the NSMutablearray allocation

I am getting the leak at this allocation
filteredListContent = [[NSMutableArray alloc] initWithCapacity:[showList count]];
CODE:
-(void)reloadTable
{
EventListAppDelegate *appDelegate;
UIApplication app = [UIApplication sharedApplication];
appDelegate = (EventListAppDelegate *)[app delegate];
contactList = [appDelegate getAllContactsList];
inviteeList = [appDelegate getInviteeListForEvent:event.primaryKey];
if (isInvited == YES)
{
showList = [appDelegate getInviteeListForEvent:event.primaryKey];
}
else
{
showList = [appDelegate getAllContactsList];
}
filteredListContent = [[NSMutableArray alloc] initWithCapacity:
[showList count]];
[filteredListContent addObjectsFromArray: showList];
[self organizeContactItemsIntoIndexes];
self.title = [event.name capitalizedString];
[self getToolbar];
[theTableView reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[filteredListContent removeAllObjects];
ContactDTO *currentElement;
NSRange range;
for (currentElement in showList)
{
range = [currentElement.lastName rangeOfString:searchText
options:NSCaseInsensitiveSearch];
if(range.location == 0)
{
[filteredListContent addObject:currentElement];
}
}
[self organizeContactItemsIntoIndexes];
[theTableView reloadData];
}
- (void)dealloc
{
[filteredListContent release];
[super dealloc];
}
Your code will allocate a new instance of filteredListContent every time reloadTable is called, which will usually happen several times during the lifetime of your application. This causes a leak because the old instances are not released.
The best (and easiest) way to fix it would be to make filteredListContent a retain property:
in your class header:
#property (nonatomic, retain) NSMutableArray * filteredListContent;
in your reloadTable method:
self.filteredListContent = [NSMutableArray arrayWithCapacity:[showList count]];
Note the use of self. in the second code snippet. That syntax informs Cocoa that it should use the property accessor to set the value of filteredListContent, which will then send the appropriate retain and release messages for you.
You've posted three nearly-identical questions pertaining to memory leaks. It might be helpful for you to read through Apple's Memory Management Programming Guide.