1

I have a UITableView that gets populated my a NSMutableArray. This is how I have it set up in my .h

@interface processViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *processList;
}

@property (copy, readwrite) NSMutableArray *processList;

and my .m

@synthesize processList;

-(void)viewDidLoad {
    processList = [[NSMutableArray alloc] init];
}

I put a NSLog on it on the viewDidLoad, and it displays just fine. But after I run a action, the processList array returns null. Any ideas why?

Thanks
Coulton

EDIT 1:

- (void)startUploads {

// Start UIActivity in the top
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// Start Pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

// Display results for testing purposes (commented out)
NSArray *resultstwo = [database executeQuery:@"SELECT * FROM processes"];
for (NSDictionary *rowtwo in resultstwo) {

    // Get ID
    int getUserIDcount = 0;
    NSArray *getUserIDInfo = [database executeQuery:@"SELECT * FROM login"];
    for (NSDictionary *getUserIDRow in getUserIDInfo) {
        getUserIDcount++;
        NSString *oneUserID = [getUserIDRow valueForKey:@"id"];
        theUserID = [NSString stringWithFormat:@"%@", oneUserID];
    }

    // If theUserID exists...
    if (getUserIDcount == 0) {
        //myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector: @selector(checkLogin) userInfo: nil repeats: NO];
    } else {


        // Get URL of image
        NSString *sqlImageUploadPathOne = @"./../Documents/";
        NSString *sqlImageUploadPathTwo = [rowtwo valueForKey:@"image"];
        NSString *getAlbumID = [rowtwo valueForKey:@"album"];
        NSString *sqlImageUploadPath = [NSString stringWithFormat:@"%@%@",sqlImageUploadPathOne,sqlImageUploadPathTwo];

        //testLabel.text = @"Uploading...";

        // Display Image in UIImageView (uploadImageHidden)
        UIImage *attemptImage = [UIImage imageNamed:sqlImageUploadPath];
        [uploadImageHidden setImage:attemptImage];



        // Upload to server
        NSData *imageData = UIImageJPEGRepresentation(uploadImageHidden.image, 90);
        NSString *urlStringOne = @"http://myflashpics.com/iphone_processes/upload.php?album=";
        NSString *urlStringTwo = @"&id=";
        NSString *urlString = [NSString stringWithFormat:@"%@%@%@%@",urlStringOne,getAlbumID,urlStringTwo,theUserID];

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        //NSLog(@"%@", returnString);

        if ([returnString rangeOfString:@"yes"].location == NSNotFound) {
            // Fail
        } else {
            // Delete image if successful
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
            NSString *documentsDirectoryPath = [paths objectAtIndex:0];
            NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:sqlImageUploadPathTwo];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:myFilePath error:NULL];
            [database executeNonQuery:@"DELETE FROM processes WHERE image=?", sqlImageUploadPathTwo];

            // Get Photo ID
            NSArray *myWords = [returnString componentsSeparatedByString:@" "];
            NSString *photoID = [myWords objectAtIndex:1]; 
            NSString *usernameID = [myWords objectAtIndex:2]; 

            NSString *defaultName = @"Photo uploaded from the flashpics iPhone application";

            // Get Thumbnail URL
            NSString *thumbnailURLOne = @"http://myflashpics.com/users/";
            NSString *thumbnailURLTwo = @"/pictures/thumbnails/";
            NSString *thumbnailURLThree = @".jpg";
            NSString *thumbnailURL = [NSString stringWithFormat:@"%@%@%@%@%@",thumbnailURLOne,usernameID,thumbnailURLTwo,photoID,thumbnailURLThree];

            // Download thumbnail
            //NSLog(@"Downloading...");
            UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailURL]]];
            //NSLog(@"%f,%f",image.size.width,image.size.height);
            NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            //NSLog(@"%@",docDir);
            //NSLog(@"saving jpeg");
            NSString *jpegFilePath = [NSString stringWithFormat:@"%@/%@_thumbnail.jpg",docDir,photoID];
            NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.2f)];//1.0f = 100% quality
            [data2 writeToFile:jpegFilePath atomically:YES];
            //NSLog(@"saving image done");
            [image release];

            // Put in database
            NSString *thumbnailEnd = @"_thumbnail.jpg";
            NSString *thumbnailLocation = [NSString stringWithFormat:@"%@%@",photoID,thumbnailEnd];

            int theCount = 0;
            NSArray *getUserIDInfotoo = [database executeQuery:@"SELECT * FROM images WHERE id=?",photoID];
            for (NSDictionary *getUserIDRowtoo in getUserIDInfotoo) {
                theCount++;
            }

            if (theCount == 0) {
                [database executeNonQuery:@"INSERT INTO images (id, name, thumbnail, album) VALUES (?, ?, ?, ?)", photoID, defaultName, thumbnailLocation, getAlbumID];
            }
            //[NSThread detachNewThreadSelector:@selector(updateImages) toTarget:RootViewController withObject:nil];
            //myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector: @selector(updateImages) userInfo: nil repeats: NO];
        }

        [request release];
        [returnString release];

    }

    //NSLog([rowtwo valueForKey:@"image"]);
    //NSLog([rowtwo valueForKey:@"album"]);
}


[pool release];

// Stop the UIActivity in the top bar
TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([dataCeter.dataTen isEqualToString:@""]) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}

Edit 2:

Where the startUploads gets called (different .m)

processViewController *processTable = [[processViewController alloc] initWithNibName:@"processView.xib" bundle:nil];
[processTable startUploads];
[processTable release];
4
  • What is "a action"? It's pretty important to know what it is you're doing between the time the array is valid and the time it is invalid. Commented Apr 10, 2011 at 0:09
  • @Marc The [cocoa] tag is for Cocoa on Mac OS. For iOS, use [cocoa-touch] instead. Commented Apr 10, 2011 at 0:13
  • @Mark W: Just posted my code that happens. Commented Apr 10, 2011 at 0:22
  • Don't see were processList gets set. Commented Apr 10, 2011 at 0:31

1 Answer 1

3

More code would be helpful but here are some suggestions:

  1. Check if startUploads is being called before the view is loaded. The view will only be loaded when it is accessed for the first time to be added to a superview.

  2. Consider initializing processList in your init method instead of viewDidLoad both to solve #1 and b/c the view can loaded & unloaded by iOS independently of the lifecycle of the viewController (depending on what other views you are displaying and whether any memory warnings occur).

  3. Make sure you are releasing processList in dealloc. You only need to release it in viewDidUnload if it is recreated and loaded in viewDidLoad.

Your code sample doesn't show when startUploads is being called and you aren't adding any items to processList so it's hard to tell if the above is relevant. Post some more code and I'll revise my answer accordingly.

good luck!

[EDIT: added example code]

The code fragments you posted are not a complete implementation of a view controller and the associated objects that interact with it. Given the code I have seen, your application design does not conform to MVC (Model/View/Controller) design pattern and I would be doing things a bit differently. However, I don't want to make assumptions about code I haven't seen or your ultimate intent or ability as a developer. I can't write your app for you, just trying to directly help you with the specific question you asked regarding why your NSMutableArray property remains null after the startUploads action completed. With that in mind, here are my edits to the code you posted:

processViewController.m - add the following:

- (id)initWithNibNamed:(NSString *)nibName bundle:(NSBundle *)bundle {
    self = [super initWithNibNamed:nibName bundle:bundle];
    if (self) {
        processList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc {
    self.processList = nil;
    [super dealloc];
}

different.m

- (void)displayProcessVC {
    ProcessViewController *processVC = [[ProcessViewController alloc] initWithNibNamed:@"processView.xib" bundle:nil];
    NSLog(@"Different:displayProcessVC BEFORE STARTING UPLOAD, processList = %@", processVC.processList);
    [processVC startUploads];
    NSLog(@"Different:displayProcessVC AFTER STARTING UPLOAD, processList = %@", processVC.processList);
    // would normally present process VC here
    [processVC release];
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

ok, so my answer is valid. your problem looks like #2. You need to allocate processList in initWithNibName:, not in viewDidLoad. The view is not loaded yet when startUploads is called. Your example doesn't make much sense b/c you are initializing processTable and never presenting it. processViewController would typically be presented modally or pushed onto a navigation controller before being released.
Ok, so if I do it like this: processTable.processList = [[NSMutableArray alloc] init];, it won't work. When the viewDidLoad it opened, it outputs NULL in the console. I also put it in my -(id)init { processList = [[NSMutableArray alloc] init]; } and it doesn't get called when ever startUploads does (put a NSLog the init and it doesn't show up until the view is opened).
When I said to consider initializing processList in your init method I didn't literally mean -(id)init. In this case you are using -(id)initWithNibName: so you need to do it there. Override the superclass implementation and initialize processList there. I recommend reading Apple's View Controller Programming Guide and specifically the sections on the View Controller life cycle.
Again, excuse my noob-ish comments. I really have only been looking and working with Xcode for over 2 months. I have looked all over and I still can't seem to find a cure to this. Do I put it in the view I am loading or the view it's loading from? Could you also provide some example code if it's not too hard. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.