use the autorelease to release,the app will crash - ios

I write an app (using no-arc)named Album which as the iPhone's native "Photo".
My Question:
1.
(please look the attached file name:1)when clicking the button of "+",then inputing some string and clicking the button of "save",the app will crash.But if change the code from "NSMutableArray *albumArr = [[[NSMutableArray alloc] init] autorelease];" to "NSMutableArray *albumArr = [[NSMutableArray alloc] init]", the app can work fine.But I think I should use the autorelease to release.
The related code:
// AlbumDB.m
+ (NSMutableArray *)fetchAlbumData
{
#warning why autorelease crash?
NSMutableArray *albumArr = [[[NSMutableArray alloc] init] autorelease];
FMDatabase *db = [FMDatabase databaseWithPath:[self dataBasePath]];
if ([db open]) {
NSString *sqlSelect = #"SELECT * FROM ALBUM";
FMResultSet *result = [db executeQuery:sqlSelect];
while ([result next]) {
AlbumModel *albumModel = [[AlbumModel alloc] init];
albumModel.albumid = [result intForColumn:#"albumid"];
albumModel.albumName = [result stringForColumn:#"albumName"];
[albumArr addObject:albumModel];
[albumModel release];
}
[db close];
}
return albumArr;
}
(please look the attached file name:2)when analyzing the code,I find the potential leak of an object.But in the dealloc,I had released.Why happen?
The related code:
//MainViewController.h
#property (nonatomic, retain) AlbumModel *editingAlbum;
// MainViewController.m
- (void)dealloc
{
[_albumArr release], _albumArr = nil;
self.editingAlbum = nil;
self.detailViewController = nil;
[super dealloc];
}

I think you should learn more about mrc.
In your first case, the albumArr if it is autorelease , it means when the runloop end ,it will be release, so the _albumArr will be nil when you use, you must retain it ,when you set the value to _albumArr.
In the second case, self.editingAlbum = [[AlbumModel alloc] init]; It will casuse the editingAlbum retain cout ==2 .You must change the code to like this:
AlbumModel *temp = [[AlbumModel alloc] init];
self.editingAlbum = temp;
[temp release];

Related

duplicate data on refresh ios

I've got my reload function reloading data properly, but I'm getting duplicate data. I've tried nil-ing out the changelist in the below spots and had no luck. Should I be nil-ing out the jsonObject? Or am I just putting it in the wrong spot.
Thanks for any help.
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to refresh"];
[refresh addTarget:self action:#selector(refreshmytable:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
NSURLSessionConfiguration *config =
[NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config
delegate:self
// delegate:nil
delegateQueue:nil];
[self fetchFeed];
}
- (void)refreshmytable:(UIRefreshControl *)refreshControl{
[self fetchFeed]; //Added 12:12 9.16.14
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:#"Updating"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *updated = [NSString stringWithFormat:#" Last Update: %#", [formatter stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:updated];
[refreshControl endRefreshing];
[self.tableView reloadData]; //Added this 11:32 9.16.14
}
- (void)fetchFeed
{
NSString *userEID = MAP_getUsername();
//NSLog(userEID);
NSString *requestString1 = [#"URL" stringByAppendingString:userEID];
NSString *requestString2 = #"&status=pending";
NSString *requestString = [requestString1 stringByAppendingString:requestString2];
//NSLog(requestString);
/*NSString *requestString = #"http://URL";
*/
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:req
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
self.changeList = jsonObject[#"List"];
//self.changeList=nil; //tried to add here to remove duplicate data
NSLog(#"%#", self.changeList);
//- add code here to populate BNRItemStore with the change order list.
// - following code should be rewritten in fetchFeed that will load BNRItemStore.
if (self.changeList.count>0) {
for (int i = 0; i < self.changeList.count; i++) {
NSDictionary *coItem = self.changeList[i];
[[BNRItemStore sharedStore]
addItemWithApproverEid:coItem[#"approverEid"]
assignmentGroup:coItem[#"assignmentGroup"]
changeOrder:coItem[#"changeOrder"]
subcategory:coItem[#"subCatagory"]
title:coItem[#"title"]
];
}
}
//NSLog(#"sizeof(NSInteger) = %#", #(sizeof(NSInteger)));
//- end comment
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
//self.changeList=nil; //trying to null out list for refresh non duplicate data
// NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(#"%#", json);
}];
[dataTask resume];
}
Added BNRITEM.h class
#class BNRItem;
#interface BNRItemStore : NSObject
#property (nonatomic, readonly) NSArray *allItems;
// Notice that this is a class method and prefixed with a + instead of a -
+ (instancetype)sharedStore;
- (BNRItem *)addItemWithApproverEid:(NSString *)aEid
assignmentGroup:(NSString *)aGroup
changeOrder:(NSString *)changeOrder
subcategory:(NSString *)subcategory
title:(NSString *)title;
#end
added BNRitem.m class
interface BNRItemStore ()
#property (nonatomic) NSMutableArray *privateItems;
#end
#implementation BNRItemStore
+ (instancetype)sharedStore
{
static BNRItemStore *sharedStore;
// Do I need to create a sharedStore?
if (!sharedStore) {
sharedStore = [[self alloc] initPrivate];
}
return sharedStore;
}
I believe the issue is in this code:
[[BNRItemStore sharedStore]
addItemWithApproverEid:coItem[#"approverEid"]
assignmentGroup:coItem[#"assignmentGroup"]
changeOrder:coItem[#"changeOrder"]
subcategory:coItem[#"subCatagory"]
title:coItem[#"title"]
You keep adding data to BNRItemStore but you don't remove the old one, there in nothing to do with self.changeList.
You need some way to remove all data before you add the new one, so on the beginning of the method fetchFeed you can call something like this:
[[BNRItemStore sharedStore] removeAllData];
Note I don't know that class BNRItemStore so removeAllData method probably doesn't exists, maybe there is another method to delete all data or maybe you nnd to implement one.
// Extended
I cannot see all of the method in .m file so I don't know where the data are stored by I believe it's stored in privateItems variable, maybe there is some method to remove all object from that array but it's not declared as public.
You can add a method definitions after
+ (instancetype)sharedStore;
in BNRITEM.h:
-(void)removeAllData;
And in BNRITEM.h implement it like that:
-(void)removeAllData {
[self.privateItems removeAllObjects];
}
And as I said before call [[BNRItemStore sharedStore] removeAllData]; at the beginning of fetchFeed method.

Singeleton - error: [CFString isNSString__] message sent to deallocated instance

I have an NSMutableArray in a singleton, which I want to access in two classes. I've done this in another project exactly like this (that one had ARC, this one doesn't) and it worked there.
Project doesn't have ARC on.
I'm getting the error:
*** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0
StoreVars.h
#interface StoreVars : NSObject
#property (nonatomic, retain) NSMutableArray *sharedArrayOfVideo;
+ (StoreVars *)sharedInstance;
#end
StoreVars.m
#implementation StoreVars
#synthesize sharedArrayOfVideo;
+ (StoreVars *) sharedInstance {
static StoreVars *myInstance = nil;
if (myInstance == nil) {
myInstance = [[[[self class] alloc] init] retain];
myInstance.sharedArrayOfVideo = [[NSMutableArray alloc] init];
}
return myInstance;
}
#end
Populating the array asynchronously:
NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:
ID,#"id",
title,#"title", nil];
[[StoreVars sharedInstance].sharedArrayOfVideo addObject:tempDict];
This is where crash happens:
NSLog(#"%i",[[StoreVars sharedInstance].sharedArrayOfVideo count]);
NSLog(#"%#",[StoreVars sharedInstance]);
if ([[StoreVars sharedInstance] respondsToSelector:#selector(sharedArrayOfVideo)]) {
**NSLog(#"%#",[StoreVars sharedInstance].sharedArrayOfVideo);**
//NSLog(#"%#",[[StoreVars sharedInstance].sharedArrayOfVideo objectAtIndex:8]);
}
Output:
10
<StoreVars: 0xb381b00>
*** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0
Found the problem, when creating the dictionary, I did:
NSString *ID = [[[arrayOfEntry objectAtIndex:index] objectForKey:#"id"]; autorelease]
NSString *title = [[[arrayOfEntry objectAtIndex:index] objectForKey:#"title"] autorelease];
Instead of:
NSString *ID = [[arrayOfEntry objectAtIndex:index] objectForKey:#"id"];
NSString *title = [[arrayOfEntry objectAtIndex:index] objectForKey:#"title"];
NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:ID,#"id",title,#"title", nil];
Try to create singleton with gcd like this:
static ASBTLEManager *sharedInstance = nil;
+ (ASBTLEManager *)sharedManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[ASBTLEManager alloc] initInstance];
});
return sharedInstance;
}
try this way to create singleton instance, this is more accurate than what you are trying
+(StoreVars *) sharedInstance
{
static StoreVars * myInstance = nil;
static dispatch_once_t oneTimePredicate;
//get calls only once.
dispatch_once(&oneTimePredicate, ^{
myInstance = [[self alloc] init];
myInstance.sharedArrayOfVideo = [[NSMutableArray alloc] init];
});
return myInstance;
}
I have a question with regards to the line below in your code:
if ([[StoreVars sharedInstance] respondsToSelector:#selector(sharedArrayOfVideo)])
Q. Why have you tried to check the mutableArray as method via selector ?
If you want to iterate through the added dictionaries do the following
StoreVars *storeVars = [StoreVars sharedInstance];
if(storeVars.sharedArrayOfVideo.count>0)
{
for(int i=0; i<storeVars.sharedArrayOfVideo.count; i++)
{
NSDictionary *dictionary = [storeVars.sharedArrayOfVideo objectAtIndex:i];
// do stuff with the grabbed dictionary.
}
}
else { NSLog(#"sharedArrayOfVideo is empty"); }
Further in your singleton creation code perform the following edit:
Instead of following:
if (myInstance == nil) {
myInstance = [[[[self class] alloc] init] retain];
Use the following:
if (myInstance == nil) {
myInstance = [[StoreVars alloc] init];
// you already own the above object via alloc/init, so no extra retains.

Singleton in static library in iOS

I have a static library which contains a Singleton class (FMDB SQLite Data Access), now I open from my main application the connection and do thingss... this works, after that a method in my library want to call the a method on my singleton and I get the error that
-[FMDatabase executeQuery:withArgumentsInArray:]: message sent to deallocated instance 0xa443960
is this not possible what I'm trying to achieve?
this is short version of my singleton
static MySingleton* _sharedMySingleton = nil;
FMDatabase *database;
#ifndef __clang_analyzer__
+(MySingleton*)sharedMySingleton
{
#synchronized([MySingleton class])
{
if (!_sharedMySingleton)
[[self alloc] init];
return _sharedMySingleton;
}
}
#endif
+(id)alloc
{
#synchronized([MySingleton class])
{
NSAssert(_sharedMySingleton == nil, #"Attempted to allocate a second instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}
}
-(Resource *)getResourceForName:(NSString *)name
{
NSString *select = #"SELECT Data, MimeType FROM File WHERE FileName = ? LIMIT 1";
NSArray *arguments = [NSArray arrayWithObject:[NSString stringWithFormat:#"/%#", name]];
FMResultSet *s = [database executeQuery:select withArgumentsInArray:arguments];
if (s == NULL)
{
FuncFileLog(#"getResourceForName file cant be loaded: %#", [database lastErrorMessage]);
return nil;
}
NSData *data = nil;
NSString *mimeType;
while ([s next])
{
data = [NSData dataFromBase64String:[s stringForColumnIndex:0]];
mimeType = [s stringForColumnIndex:1];
}
Resource *resource = [[[Resource alloc] initWithData:data mimeType:mimeType] autorelease];
return resource;
}
-(BOOL)openDatabase
{
database = [FMDatabase databaseWithPath:[self getDocumentResourcePath]];
return [database open];
}
-(void)closeDatabase
{
[database close];
[database release];
}
-(void)dealloc
{
if (database != NULL)
{
[self closeDatabase];
}
[baseUrl release];
[super dealloc];
}
#end
EDIT:
I found that the dealloc from FMDatabase gets called after my application start return, but dont know why.
EDIT2:
Currently I thought one problem was this line
database = [FMDatabase databaseWithPath:[self getDocumentResourcePath]];
here I have to retain the object.
You don't actually assign the singleton instance:
if (!_sharedMySingleton)
[[self alloc] init];
should be:
if (!_sharedMySingleton)
_sharedMySingleton = [[self alloc] init];
and dump that overridden alloc method.
Also database should be an instance variable in the class, and not at global scope.

How can I lower memory climb when generating large PDF's [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cannot create PDF document with 400+ pages on iOS
My app generates PDF files. These PDf's can be large due to the user being able to add pages, potentially unlimited, although usual is approx. 10. Im having problems with iPhone 4 users experiencing a crash at the PDF genration stage. Some investigative work shows the app is running out of memory during the PDF generation stage, I get low memory warnings then final crash. I can reproduce the problem If I add 50 plus pages on iPhone 5, a lot less on iPhone 4, and not on the simulator which is expected.
Can anyone suggest how I can reduce this aculamitive memory climb and ultimate crash when generating the PDf file.
Ive researched SO: iPhone App Crashes due to Low Memory but works fine in simulator and Quartz PDF API Causing Out of Memory Crashes
#interface ICPDFPreviewController ()
#property (nonatomic, strong) Certificate *certificate;
#property (nonatomic, strong) NSData *pdfData;
#property (nonatomic) BOOL viewHasUnloaded;
- (void)generatePdf;
- (void)pdfDone:(NSData *)data;
- (NSData *)createPdfWithPages:(NSArray *)pages;
#end
#implementation ICPDFPreviewController
#synthesize certificate=_certificate;
#synthesize scrollView=_scrollView;
#synthesize webView=_webView;
#synthesize pdfData=_pdfData;
#synthesize viewHasUnloaded=_viewHasUnloaded;
- (void)generatePdf
{
NSMutableArray *pagesArray = [NSMutableArray array];
if ([self.certificate.certificateType.title isEqualToString:#"Minor Works"]) {
[pagesArray addObject:[[ICPDFMinorWorksPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFMinorWorksPage2 alloc] initWithCertificate:self.certificate]];
} else if ([self.certificate.certificateType.title isEqualToString:#"EIC"]) {
[pagesArray addObject:[[ICPDFEICPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage2 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage3 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage4 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICPage5 alloc] initWithCertificate:self.certificate]];
[self addDistributionBoardsToPagesArray:pagesArray];
ICPDFEICPageFinal *pageFinal = [[ICPDFEICPageFinal alloc] initWithCertificate:self.certificate];
pageFinal.pageNumber.text = [NSString stringWithFormat:#"%d", pagesArray.count+1];
[pagesArray addObject:pageFinal];
} else if ([self.certificate.certificateType.title isEqualToString:#"Domestic EIC"]) {
[pagesArray addObject:[[ICPDFDomesticEICPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFDomesticEICPage2 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFDomesticEICPage3 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFDomesticEICPage4 alloc] initWithCertificate:self.certificate]];
[self addDistributionBoardsToPagesArray:pagesArray];
[pagesArray addObject:[[ICPDFDomesticEICPageFinal alloc] initWithCertificate:self.certificate]];
} else if ([self.certificate.certificateType.title isEqualToString:#"EICR"]) {
[pagesArray addObject:[[ICPDFEICRPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRPage2 alloc] initWithCertificate:self.certificate]];
[self addObservationsToPagesArray:pagesArray];
[self addDistributionBoardsToPagesArray:pagesArray];
[pagesArray addObject:[[ICPDFEICRInspection alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRInspectionPage1 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRInspectionPage2 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRInspectionPage3 alloc] initWithCertificate:self.certificate]];
[pagesArray addObject:[[ICPDFEICRPageFinal alloc] initWithCertificate:self.certificate]];
}
// Set page count on all pages
int pageNumber = 0;
for (ICCertificateComponent *page in pagesArray) {
page.pageNumber.text = [NSString stringWithFormat:#"%d", ++pageNumber];
page.pageCount.text = [NSString stringWithFormat:#"%d", pagesArray.count];
}
NSData *pdfData = [self createPdfWithPages:pagesArray];
[self performSelectorOnMainThread:#selector(pdfDone:) withObject:pdfData waitUntilDone:YES];
}
- (void)pdfDone:(NSData *)data
{
self.pdfData = data;
[self.webView loadData:self.pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
[ICUtils removeProgressView];
}
- (NSData *)createPdfWithPages:(NSArray *)pages
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
ICCertificateComponent *firstPage = [pages objectAtIndex:0];
UIGraphicsBeginPDFContextToData(pdfData, firstPage.contentView.bounds, nil);
for (int i = 0; i < pages.count; i++) {
ICCertificateComponent *thisPage = [pages objectAtIndex:i];
UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);
////////////////////////////////////////////////////////////////////
//tried adding this after research on SO, did not stop app crash
// CGContextSetInterpolationQuality((__bridge CGContextRef)(thisPage), kCGInterpolationHigh); CGContextSetRenderingIntent((__bridge CGContextRef)(thisPage), kCGRenderingIntentDefault);
/////////////////////////////////////////////////////////////////////
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[thisPage.contentView.layer renderInContext:pdfContext];
}
UIGraphicsEndPDFContext();
return pdfData;
}
- (void)addDistributionBoardsToPagesArray:(NSMutableArray *)pagesArray
{
int pageCount = pagesArray.count;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"createdAt" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSArray *boards = [self.certificate.distributionBoards sortedArrayUsingDescriptors:sortDescriptors];
for (DistributionBoard *thisBoard in boards) {
DebugLog(#"Creating a board page");
ICPDFDistributionBoard *boardPage = [[ICPDFDistributionBoard alloc] initWithDistributionBoard:thisBoard];
boardPage.pageNumber.text = [NSString stringWithFormat:#"%d", ++pageCount];
DebugLog(#"Page number is %d", pageCount);
[pagesArray addObject:boardPage];
NSSortDescriptor *circuitDescriptor = [[NSSortDescriptor alloc] initWithKey:#"createdAt" ascending:YES];
NSArray *circuitDescriptors = [[NSArray alloc] initWithObjects:circuitDescriptor, nil];
NSArray *circuits = [thisBoard.circuits sortedArrayUsingDescriptors:circuitDescriptors];
//int circuitCount = circuits.count;
ICPDFCircuitDetails *circuitDetails = boardPage.circuitDetails;
int circuitCount = 0;
for (Circuit *thisCircuit in circuits) {
circuitCount++;
if (circuitCount > 16) {
// Add an extension page
DebugLog(#"Adding an extension sheet");
circuitCount = 1;
ICPDFDistributionBoardExtension *boardExtension = [[ICPDFDistributionBoardExtension alloc] initWithDistributionBoard:thisBoard];
[pagesArray addObject:boardExtension];
boardExtension.pageNumber.text = [NSString stringWithFormat:#"%d", ++pageCount];
circuitDetails = boardExtension.circuitDetails;
}
NSString *key = [NSString stringWithFormat:#"circuitRow%d", circuitCount];
ICCircuitRow *circuitRow = [circuitDetails valueForKey:key];
[circuitRow populateFromCircuit:thisCircuit];
}
}
}
In general, memory is finite and your generated output isn't, so the way to make it work is to ensure that:
you're not accumulating the entire PDF in memory as you generate it
you're not unnecessarily keeping around byproducts of the rendering for each page
In your case, using UIGraphicsBeginPDFContextToData means that you're rendering the whole PDF to an ever-expanding NSData. When that data gets too big, you'll get killed. Instead, try UIGraphicsBeginPDFContextToFile. Also in your inner loop for rendering pages, consider inserting an #autoreleasepool { ... } block to prevent objects from building up unnecessarily during a long run. I'm not sure how big your pagesArray bunch of stuff actually is, and whether that's something you might consider "paging in" one page at a time as you generate.
EDIT: I think Ben Zotto's solution is the way to go: use UIGraphicsBeginPDFContextToFile.
Someone asked this months ago. I wrote a memory mapped data consumer that might help:
https://gist.github.com/3748250
It uses a memory mapped PDF context instead of normal memory.

When NSDictionary is added to array, it replaces the previous dictionary

UPDATE: Figured out some stuff and changed code.
When I add my NSDictionary to my array it suddenly replaces the previous dictionary I added last time. I don't know why this is happening. I am using a plist as data storage.
I get a error message like this:
Thread 1:Program received signal: "EXC_BAD_ACCESS".
Init
-(id)init{
self=[super init];
if(self){
dbArray = [[NSMutableArray alloc] init];
}
return self;
}
Adding a new item.
-(void)addNewItem:(NSString *)aString
{
// Creates a mutable dictionary with a anonymous string under the NAME key.
NSDictionary *newString = [[NSDictionary alloc] initWithObjectsAndKeys:aString,#"name", nil];
// Adds the new string to empty dbArray.
[dbArray addObject:(newString)];
NSLog(#"[add]:Added anonymous string to dbArray, under name key.");
// Writes the current dbArray (with the dict) to plist and releases retain counts.
[self writeItem];
[newString release];
}
My method to view my data.
-(void)viewData
{
// View data from the created plist file in the Documents directory.
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:#"data.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:finalPath]) {
self.dbArray = [NSMutableArray arrayWithContentsOfFile:finalPath];
}
else {
self.dbArray = [NSMutableArray array];
}
}
instead this
self.dbArray = [[NSMutableArray alloc] init];
use this
if( nil == self.dbArray ) {
self.dbArray = [[NSMutableArray alloc] init];
}
UPDATE: (based on provided code)
you're using different instances of DataObject class for displaying & saving data. Your content is over-written, because you don't load data from file during initialization of each instance; to fix that fast, you need to implement init method of your DataObject class as below:
- (id)init{
self = [super init];
if(self){
[self viewData];
}
return self;
}
the following code from viewDidLoad of ViewController class will crash your application very often:
db = [[DataObject alloc] init];
[db viewData];
[db release];
array = [[NSMutableArray alloc] initWithArray:[db dbArray]];
replace it with
db = [[DataObject alloc] init];
[db viewData];
array = [[NSMutableArray alloc] initWithArray:[db dbArray]];
call [db release] only in dealloc implementation
another problem, that you'll probably arise - is updated data is not displayed when you're back to the main screen; to fix that add the following method implementation to your ViewController.m file:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[db viewData];
self.array = [NSMutableArray arrayWithArray: db.dbArray];
[self.tableView reloadData];
}
also in AddView.m replace the following code
// Dismiss view and reload tableview.
ViewController *vc = [[ViewController alloc] init];
[self dismissModalViewControllerAnimated:YES];
[vc release];
with
// Dismiss view and reload tableview.
[self dismissModalViewControllerAnimated:YES];
Just as advise: see more information about using delegates and passing object instances & copies between objects.
I think you are creating a new Array:
self.dbArray = [[NSMutableArray alloc] init];
You should create the dbArray on the viewDidLoad or on the init of your UIViewController (I am assuming you are using this on an UIViewController)
inside your DataObject do the following:
-(id)init{
self=[super init];
if(self){
self.dbArray = [[NSMutableArray alloc] init];
}
return self;
}

Resources