I'm working on my first iPhone app. My UI is composed of 4 tabs. The first tab shows active projects. The last tab is a "settings" tab where user can toggle projects on/off. The settings tab has a NSMutableArray of projects (retrieved from a webservices call) and I write them out to a file in the Documents folder for the app. The first tab reads the projects from the file by reinstating the file into a NSMutableArray and only showing the projects with an IsActive bit set to true. Everything is working, except when I trip a project's status in the settings tab then immediately click on the first tab. The first tab is not reflecting the changes made in the setting开发者_如何学编程s tab. However, if I click on one of the other tabs before going to the first tab, then the first tab reflects the changes made in the settings tab appropriately. The only thing I can think of is the file is not finished writing to the Documents folder when going directly from settings tab to the first tab. I'm writing the NSMutableArray in the ViewDidDisapper event for the Settings tab. What am I missing? Thanks for your help.
Unless your data sets are large, I find it's much better to hold all data in memory, and only write to disk on app terminate/to-background and read from disk on app launch. Create a singleton "FooManager" class to hold data required in different parts of your app, and access the data through the API of the singleton class. I think this is much cleaner than hanging everything off of ProjectAppDelegate. This should also solve your write/read race condition.
EDIT: here's a tiny little singleton network image cache manager class I'm using in a current project. Sometimes an example's worth a thousand words of tutorials :). In order to use this class, in any code, just #import the header, and do:
NetworkImageCacheManager *nicm = [NetworkImageCacheManager sharedInstance];
UIImage *img = [nicm imageWithURL:imageURL];
Here's the code for this class:
#import <Foundation/Foundation.h>
@interface NetworkImageCacheManager : NSObject
{
NSMutableDictionary *imgCache;
}
@property (nonatomic, retain) NSMutableDictionary *imgCache;
+ (NetworkImageCacheManager *) sharedInstance;
- (UIImage *) imageWithURLString:(NSString *)imgURLString;
- (void) setImage:(UIImage *)theImage forURLString:(NSString *)imgURLString;
@end
@implementation NetworkImageCacheManager
@synthesize imgCache;
- (id) init
{
self = [super init];
if ( self )
{
self.imgCache = [NSMutableDictionary dictionary];
}
return self;
}
+ (NetworkImageCacheManager *) sharedInstance
{
static NetworkImageCacheManager *g_instance = nil;
if ( g_instance == nil )
{
g_instance = [[self alloc] init];
}
return g_instance;
}
- (UIImage *) imageWithURLString:(NSString *)imgURLString
{
UIImage *rv = [self.imgCache objectForKey:imgURLString];
return rv;
}
- (void) setImage:(UIImage *)theImage forURLString:(NSString *)imgURLString
{
[self.imgCache setObject:theImage forKey:imgURLString];
}
- (void) dealloc
{
[imgCache release];
[super dealloc];
}
@end
精彩评论