Why is my value passed through NSNotifcationCenter not preserved? - objective-c

I'm trying to send a CGPoint through an NSNotification like this
-(void)setPosition:(CGPoint)point
{
NSString *pointString = NSStringFromCGPoint(point);
NSDictionary *dict = [[NSDictionary alloc]
initWithObjectsAndKeys:#"p", pointString, nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"BownceSpriteDidSetPosition"
object:self
userInfo:dict];
[super setPosition:CGPointMake(point.x, point.y)];
}
And I've implemented the observer like this
-(void) init
{
if((self = [self init])){
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(setViewPointCenter:)
name:#"BownceSpriteDidSetPosition"
object:nil];
// I wondered wether 'object' should be something else???
// more code etc....
}
return self
}
-(void) setViewPointCenter:(NSNotification *)notification
{
NSString * val = [[notification userInfo] objectForKey:#"p"];
CGPoint point = CGPointFromString(val);
// trying to debug
NSString debugString = [NSString stringWithFormat:#"YPOS -----> %f", point.y];
NSLog(debugString);
CGPoint centerPoint = ccp(240, 160);
viewPoint = ccpSub(centerPoint, point);
self.position = viewPoint;
}
But it seems that CGPoint is empty, or (0,0) maybe. Either way, it's not having the desired effect, and the debugString is showing point.y to be 0.0.
From all the examples I've found, it looks to me like I'm doing it all right. But obviously I'm not. Can anyone nudge me in the right direction and point out my mistake?

You've got your objects and keys reversed in the dictionary. It should read
NSDictionary *dict = [[NSDictionary alloc]
initWithObjectsAndKeys:pointString,#"p", nil];
Yes, it's exactly backwards of the way you would expect it to be and this bites me about every third time I create a dictionary.

Your problem is here:
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:#"p", pointString, nil];
It should be:
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:pointString, #"p", nil];
"Objects" comes before "Keys" in the selector, so you list your items as ObjectA, KeyForObjectA, ObjectB, KeyForObjectB, etc.
You're also leaking this dictionary, since you alloc/init it, but never release it (I'm assuming you're not using garbage collection).

In new objective-c syntax is better to use:
NSDictionary *dict = #{#"p": [NSValue valueWithCGPoint:point]};
it easier to understend and it use NSValue instead of NSString.
There is also a problem with removing observer. In your code, you only use [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(setViewPointCenter:) name:#"BownceSpriteDidSetPosition" object:nil]; but never call [[NSNotificationCenter defaultCenter] removeObserver:self];, whitch can produce nasty crash, that would be hard to debug. I sugest you using library https://github.com/AllinMobile/AIMObservers that prevents this kind of crash. You could rewrite your code in that way:
__weak __typeof(self) weakSelf = self;
self.observer = [AIMNotificationObserver observeName:#"BownceSpriteDidSetPosition" onChange:^(NSNotification *notification) {
NSValue *valueOfPoint = [notification userInfo][#"p"];
CGPoint point = [valueOfPoint CGPointValue];
CGPoint centerPoint = ccp(240, 160);
viewPoint = ccpSub(centerPoint, point);
//use weakSelf to avoid strong reference cycles
weakSelf.position = viewPoint;
}];

Related

Where to create a NSManagedObjectContext for an NSInvocationOperation

I have multiple NSInvocationOperations created and added to an NSOperationQueue. Two of these
NSInvocationOperations create lots of objects of the same parent class (Country and City which subclass Location). It has mostly gone well except that I've noticed changes to one model or the other are kinda clobbered.
Looking at the store (using a sqlite program) I see the first City (of maybe 200 total) created and then all of the Countries (again maybe 200) created. If I delete the app and run it again I'll see the first Country and then all of the Cities.
I hit the docs and noticed that Apple suggestions setting up your per thread MOCs in the start method of you NSOperation. However I'm not using an NSOperation, I'm using an NSInvocationOperation. It's actually making me question more so why they suggest creating your MOC in start.
This is my selector for my NSInvocationOperation...
+ (void)load:(NSString *)file
{
NSManagedObjectContext *managedObjectContext = [(OSSMAppDelegate *)[[UIApplication sharedApplication] delegate] adHocManagedObjectContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:managedObjectContext];
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSString *json = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:#"json"]];
NSArray *objects = [[jsonParser objectWithString:json] valueForKeyPath:#"objects"];
for(NSDictionary *object in objects)
{
[self createObjectWithObject:object inManagedObjectContext:managedObjectContext];
}
NSError *error = nil;
[managedObjectContext save:&error];
}
...from the app delegate...
- (NSManagedObjectContext *)adHocManagedObjectContext
{
NSManagedObjectContext *adHocManagedObjectContext = nil;
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
adHocManagedObjectContext = [[NSManagedObjectContext alloc] init];
[adHocManagedObjectContext setPersistentStoreCoordinator:coordinator];
[adHocManagedObjectContext setUndoManager:nil];
}
return adHocManagedObjectContext;
}
...then somewhere else (Note: firstRun calls load:)...
NSInvocationOperation *countryInvocationOperation = [[NSInvocationOperation alloc] initWithTarget:[Country class] selector:#selector(firstRun) object:nil];
[operationQueue addOperation:countryInvocationOperation];
Is there any problem with creating the MOC in the selector that's being invoked? I'd image it has to be since the MOC is tied to the thread it's created on. I guess any pointers as to where I'm going wrong is helpful.
I'm not sure I understand your problem (Do you have missing countries or cities?, do you have incorrect order? give an example of 'clobbered').
As for your question:
Is there any problem with creating the MOC in the selector that's being invoked?
No, there is no problem. the documentation only say it must be created ON the thread you intend to use it (start and main are methods that will run on the operation thread). hence, NSInvocationOperation will run your method in the operation thread, and you can create your MOC there without worries.

Objective-C: Multiple Views and Data Coming From An NSTask

So, I managed to get NSTask to read asynchronously from a program, but I did it inside the class of a UIView in my storyboard. (Not an Obj-C expert)
My ideia is: I read the text from the program place it on a UITextView and then when there's more repeat the process via NSNotificationCenter
So far this is my code:
LView.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSPipe *out_pipe = [NSPipe pipe];
sshoutput = [out_pipe fileHandleForReading];
[sshoutput readInBackgroundAndNotify];
utilT = [[NSTask alloc] init];
[utilT setLaunchPath:#"/usr/bin/utilfc9"];
[utilT setArguments:[NSArray arrayWithObjects: #"-p", #"-f", #"log.txt", nil]];
[utilT setStandardOutput: out_pipe];
[utilT setStandardError: out_pipe];
[utilT launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(readPipe:) name:NSFileHandleReadCompletionNotification object:nil];
}
-(void)readPipe: (NSNotification *)notification
{
NSData *data;
NSString *new_input;
if( [notification object] != sshoutput ) { return };
data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
new_input = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
self.log.text = [self.wifilog.text stringByAppendingFormat: #"\n%#", new_input];
if( utilT ) {
[sshoutput readInBackgroundAndNotify];
}
}
LView.h:
#import <UIKit/UIKit.h>
#import "NSTask.h"
NSTask *sshT;
NSFileHandle *sshoutput;
So far it works great, I get the data live without any issues.
But, how can I place this NSTask in a more "global" place like AppDelegate's application didFinishLaunchingWithOptions and then process the data and update multiple views in another classes? I tried and of course I can stuff like log.text = new_input inside AppDelegate because it's from another class, and including it does not solve the problem.
As you might noticed, I'm not interested in sending this to the AppStore. It's an app for myself to use on a jailbroken iPhone.
Thank you.
Quick way to do it is
In all of the Views that you want to recieve this same notification add the following
ReceiverView
-(void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(read:) name:#"ReadTest" object:nil];
}
//read function
-(void) read:(NSNotification*)notification
{ // Do something with the notification }
Now in LView.m
-(void)readPipe: (NSNotification *)notification
{
NSData *data;
NSString *new_input;
if( [notification object] != sshoutput ) { return };
data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
new_input = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
self.log.text = [self.wifilog.text stringByAppendingFormat: #"\n%#", new_input];
if( utilT ) {
[sshoutput readInBackgroundAndNotify];
}
//Add the following
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReadTest" object:notification]
}
Be carefull, new_input is allocated but not released => Memory Leak

My NSMutableArray loses its objects outside of function scope even after alloc / init

I'm totally stumped on this one. I have an NSMutableArray which is declared in my header and set as a property, synthesized etc. I then call a function that allocates and initializes the array, and I add custom objects to it. I do a for each loop after the objects are added to ensure that they are actually contained within the array and they are. Once the program goes outside of this function scope, though, suddenly the array is empty.
header file:
#interface ScheduleViewController : UITableViewController {
NSString *login_id;
NSMutableArray *events;
}
- (id)initWithID:(NSString*)l_id;
- (void)grabURLInBackground; // ASIHTTP example method
#property (nonatomic, retain) NSString *login_id;
#property (nonatomic, retain) NSMutableArray *events;
#end
implementation:
#synthesize events;
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *response = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *eventDics = [parser objectWithString:response error:nil]; // an array of dictionaries of events
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
// Allocate empty event object and initialize the mutable array
Event* event = [[Event alloc] init];
self.events = [[NSMutableArray alloc] initWithCapacity:[eventDics count]];
// loop through the array of dictionaries
for (int i = 0; i < [eventDics count]; i++)
{
NSDictionary *dict = [eventDics objectAtIndex:i];
for(NSString *key in dict) {
// for the sake of readability i wont include the code
// but the event is populated here
}
[self.events addObject:event];
[event release];
}
NSLog(#"Array Count: %i", [self.events count]);
for (Event *e in events) {
NSLog(#"eventid: %i, type: %#, price: %f, name: %#", e.event_id, e.type, e.price, e.name);
}
[parser release];
[dateForm release];
}
So the above code works fine and prints out the variables from the Event objects that are stored in the events mutable array.
What I want to do is use the events array in another function now, and when I try to, the count is 0 and also no objects are stored in the array when I look at it.
In viewDidUnload I set self.events = nil; and in dealloc I do [self.events release]
You are doing your alloc/init for the Event *event object outside of your for loop. This means you are adding the same object every time you add it to the array. You should move this line:
Event* event = [[Event alloc] init];
To the inside of your
for (int i=0 ... loop.
Not sure that would explain the symptoms you are seeing, but it could, since the following statement:
[event release]
is also releasing that one allocated object once for every time through the loop - so you are releasing the object multiple times. If you move the Event alloc to the inside of the loop then the release will be ok. (adding the object to the array will retain it so its ok to release it, but you need to allocate a new Event each time through the loop).
Basically your code should look like this: (note I've also added an autorelease to your array alloc).
// Allocate empty event object and initialize the mutable array
self.events = [[[NSMutableArray alloc] initWithCapacity:[eventDics count]] autorelease]; // assigning to the retain property will retain it, so autorelease it or it will be retained twice. Could also have used the arrayWithCapacity convenience method here instead and then wouldn't need to autorelease.
// loop through the array of dictionaries
for (int i = 0; i < [eventDics count]; i++)
{
Event* event = [[Event alloc] init]; // Allocate a new Event each time through the loop so you are adding a unique object to the array each time.
NSDictionary *dict = [eventDics objectAtIndex:i];
for(NSString *key in dict) {
// for the sake of readability i wont include the code
// but the event is populated here
}
[self.events addObject:event];
[event release];
}
I see many problems with this code. These include the fact that you are releasing objects at inappropriate times, and that you are getting confused about the scope of different objects. It seems to me that one of the biggest problems that you are having is re-allocating your events array every time the requestFinished: method is called. In your init method, you should do something like this:
- (id)init {
if ((self = [super init])) {
// Since it is a retain property, we should autorelease it when
// assigning to it, thus preventing an extra retain.
self.events = [[[NSMutableArray alloc] initWithCapacity:[eventDics count]] autorelease];
}
}
With that being said, here is how I would rewrite your requestFinished: method, as well as your dealloc method:
- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *response = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *eventDics = [parser objectWithString:response error:nil]; // an array of dictionaries of events
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
// Clear the already allocated events array
[self.events removeAllObjects];
for (int i = 0; i < [eventDics count]; i++) {
// note how I assign event in here
Event *event = [[Event alloc] init];
NSDictionary *dict = [eventDics objectAtIndex:i];
for (NSString *key in dict) {
// Do whatever it is you do here
}
[self.events addObject:event];
[event release];
}
NSLog(#"Array Count: %i", [self.events count]);
for (Event *e in events) {
NSLog(#"eventid: %i, type: %#, price: %f, name: %#", e.event_id, e.type, e.price, e.name);
}
[parser release];
[dateForm release];
}
Finally, you can simply set the events property to nil in the dealloc method:
- (void)dealloc {
self.events = nil;
[super dealloc];
}
The only reason that I can think of for the array being empty is that a) it's contents are being deallocated, or b) it itself is being deallocated and set to nil. The pieces of your code that I fixed could possibly cause both of these. Try the changes that I have made, and see if they make a difference.
So I figured out the problem and it's due to an error on my part. After stepping through the function calls more closely, it turns out that the table view delegate method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
was being called before populating the array with requestHasFinished. I was calling requestHasFinished indirectly through viewDidLoad but I think that the table view delegate method was being called when the view controller is initialized. Init was being called before viewDidLoad because the view controller is actually handled within a tab view controller which initializes all of the view controllers for each tab at the time when itself is initialized. That's another matter to get into.. I'm not sure if I like everything being initialized and setup before the views are even displayed but.. something to research.
Anyways thanks again for the help.

Objective C - UITableView after calling reloadData my object properties are null/nil

I have a ViewController defined as follows:
#interface SectionController : UITableViewController {
NSMutableArray *sections;
}
- (void) LoadSections;
When LoadSection is call it makes a call to NSURLConnection to load a url which in turn calls
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[connection release];
[responseData release];
NSDictionary *results = [responseString JSONValue];
NSMutableArray *jSections = [results objectForKey:#"Items"];
sections = [NSMutableArray array];
for (NSArray* jSection in jSections)
{
Section* section = [Section alloc];
section.Id = [jSection objectForKey:#"Id"];
section.Description = [jSection objectForKey:#"Description"];
section.Image = [jSection objectForKey:#"Image"];
section.Parent = [jSection objectForKey:#"Parent"];
section.ProductCount = [jSection objectForKey:#"ProductCount"];
[sections addObject:section];
[section release];
}
[jSections release];
[results release];
[delegate sectionsLoaded];
[self.view reloadData];
}
The data parses correctly and I now have sections filled with many items.
Calling [self.view reloadData] forces a callback to the delegate method cellForRowAtIndexPath which should then present the data into the cell however its at this point that sections is now nil again.
Can someone please point out my mistake? I must admit I am a newbie to objective c and it probably a pointer issue. What is need to do is retain the value of sections after calling reloadData.
Many thanks.
Seeing the new code the problem is obvious:
sections = [NSMutableArray array];
should become
[sections release];
sections = [[NSMutableArray alloc] init];
note that the array does not become again "nil", is instead deallocated and you get an invalid reference, which might (should) generate a crash on dereferencing.
I suggest you to read some articles on reference counted memory management as it might be not obvious if you are new to Objective-C, and often leads to mistake (i.e: autorelease is not magic at all)
best way to avoid all memory leaks here is just simply use #property (nonatomic, retain) NSMutableArray *sections; by using property you can be sure that all men management works will be correctly managed by system. Just don't forget that property retains value when you doing setSections:, so that you need to pass autoreleased object here.
self.sections = [NSMutableArray array];
...
[self.sections addObject:section];
Also to avoid all problem try to make all objects which should live only in this method autorelease. Like this:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSDictionary *results = [responseString JSONValue];
NSMutableArray *jSections = [results objectForKey:#"Items"];
self.sections = [NSMutableArray array];
for (NSArray* jSection in jSections) {
Section* section = [[[Section alloc] init] autorelease];
section.Id = [jSection objectForKey:#"Id"];
section.Description = [jSection objectForKey:#"Description"];
section.Image = [jSection objectForKey:#"Image"];
section.Parent = [jSection objectForKey:#"Parent"];
section.ProductCount = [jSection objectForKey:#"ProductCount"];
[self.sections addObject:section];
}
[delegate sectionsLoaded];
[self.view reloadData];
}
And also most of object you trying to release already autoreleased:
all params passed into your method shouldn't be released manually, check I think JSONValue also should returns autoreleased object and anything you getting by enumerating or by call objectForKey:

Adding an object to an array read from a file

There is nothing in the file, but I add row1 to my array right after. NSLog tells me that the array is empty. Why isn't row1 added to the array? All of my other code is fine as far as I can tell. My app worked when I put hard values into the array. Now that I'm loading from a file, it doesn't work.
- (void)viewDidLoad {
//array value
//NSMutableArray *array;
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSMutableArray *array/*array*/ = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"Study", #"Task", #"2 hours", #"Length", #"4", #"Hours", #"0", #"Minutes", #"15", #"Tiredness", nil];
[array addObject: row1];
self.tasks = array;
NSLog(#"The contents of the array (file exists) is %#", array);
[array release];
[myTableView reloadData];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
}
Please help!
Thanks in advance,
Matt
Two possibilities I see:
The object containing this code is not correctly set up as the table's datasource.
A file exists at the path you're looking at, but its contents cannot be parsed as an array. In this case, you would hit the first branch of the if-clause, but initWithContentsOfFile: would return nil. You could easily diagnose this by checking for nil after calling that method.
make sure the tableview delegate and datasource is set properly
It is difficult to answer your question. Here are two suggestions:
When you post code here, clean it up. Remove comments and format it nicely.
Show all your code. In your code there could be many things wrong. Maybe the self.tasks property is not correct. Maybe you did not correctly setup the dataview. Maybe you did not implement the correct table view delegate methods. It is hard to tell.
Also, try ruling out the most basic things. For example if you are in doubt whether that array is setup correctly, then simply print it:
NSLog(#"The contents of the array is %#", array);