Search Results

Search found 39 results on 2 pages for 'kvo'.

Page 1/2 | 1 2  | Next Page >

  • iOS - Unit tests for KVO/delegate codes

    - by ZhangChn
    I am going to design a MVC pattern. It could be either designed as a delegate pattern, or a Key-Value-Observing(KVO), to notify the controller about changing models. The project requires certain quality control procedures to conform to those verification documents. My questions: Does delegate pattern fit better for unit testing than KVO? If KVO fits better, would you please suggest some sample codes?

    Read the article

  • Tool for debugging KVO/Bindings in Cocoa

    - by Raphael Sebbe
    Is there any tool to log current observers of a given object, in the spirit of what "gdb info gc-roots" does? I found "gdb info gc-references" could do that (sort of) as a by-product of its original purpose, but that won't work with non garbage collected apps. Thanks!

    Read the article

  • Is this a good KVO-compliant way to model a mutable to-many relationship?

    - by andyvn22
    Say I'd like a mutable, unordered to-many relationship. For internal optimization reasons, it'd be best to store this in an NSMutableDictionary rather than an NSMutableSet. But I'd like to keep that implementation detail private. I'd also like to provide some KVO-compliant accessors, so: - (NSSet*)things; - (NSUInteger)countOfThings; - (void)addThings:(NSSet*)someThings; - (void)removeThings:(NSSet*)someThings; Now, it'd be convenient and less evil to provide accessors (private ones, of course, in my implementation file) for the dictionary as well, so: @interface MYClassWithThings () @property (retain) NSMutableDictionary* keyedThings; @end This seems good to me! I can use accessors to mess with my keyedThings within the class, but other objects think they're dealing with a mutable, unordered (, unkeyed!) to-many relationship. I'm concerned that several things I'm doing may be "evil" though, according to good style and Apple approval and whatnot. Have I done anything evil here? (For example, is it wrong not to provide setThings, since the things property is supposedly mutable?)

    Read the article

  • GDB says that a KVO observer is registered even though it is not (or is it?).

    - by Paperflyer
    When my application is closed, the main controller class removes itself as Observer from the model and then releases the model. Like this: - (void)dealloc { [theModel removeObserver:self forKeyPath:@"myValue"]; [theModel release]; [super dealloc]; } And right after that, the debugger says: 2010-04-29 14:07:40.294 MyProgram[13678:a0f] An instance 0x116f2e880 of class TheModel was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: <NSKeyValueObservationInfo 0x100288450> ( <NSKeyValueObservance 0x1002aca90: Observer: 0x116f40ec0, Key path: myValue, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x116f80430> ) where 0x116f2e880 is indeed the model and 0x116f40ec0 is indeed the controller. How can the controller still be an observer when it just removed itself as an observer?

    Read the article

  • iPhone OS: KVO: Why is my Observer only getting notified at applicationDidfinishLaunching

    - by nickthedude
    I am basically trying to implement an achievement tracking setup in my app. I have a managedObjectModel class called StatTracker to keep track of all sorts of stats and I want my Achievement tracking class to be notified when those stats change so I can check them against a value and see if the user has earned an achievement. I've tried to impliment KVO and I think I'm pretty close to making it happen but the problem I'm running into is this: So in the appDelegate i have an Ivar for my Achievement tracker class, I attach it as an observer to a property value of my statTracker core data entity in the applicationDidFinishLaunching method. I know its making the connection because I've been able to trigger a UIAlert in my AchievementTracker instance, and I've put several log statements that should be triggered whenever the value on the StatTracker's property changes. the log statement appears only once at the application launch. I'm wondering if I'm missing something in the whole object lifecycle scheme of things, I just don't understand why the observer stops getting notified of changes after the applicationDidFinishLaunching method has run. Does it have something to do with the scope of the AchievementTracker reference or more likely the reference to my core data StatTracker is going away once that method finishes up. I guess I'm not sure the right place to place these if that is the case. Would love some help. Here is the code where I add the observer in my appDidFinishLaunching method: [[CoreDataSingleton sharedCoreDataSingleton] incrementStatTrackerStat:@"timesLaunched"]; achievementsObserver = [[AchievementTracker alloc] init]; StatTracker *object = nil; object = [[[CoreDataSingleton sharedCoreDataSingleton] getStatTracker] objectAtIndex:0]; NSLog(@"%@",[object description]); [[CoreDataSingleton sharedCoreDataSingleton] addObserver:achievementsObserver toStat:@"refreshCount"]; here is the code in my core data singleton: -(void) addObserver:(id)observer toStat:(NSString *) statToObserve { NSLog(@"observer added"); NSArray *array = [[NSArray alloc] init]; array = [self getStatTracker]; [[array objectAtIndex:0] addObserver:observer forKeyPath:statToObserve options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; } and my AchievementTracker: - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"achievemnt hit"); //NSLog("%@", [change description]); if ([keyPath isEqual:@"refreshCount"] && ((NSInteger)[change valueForKey:@"NSKeyValueObservingOptionOld"] == 60) ) { NSLog(@"achievemnt hit inside"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"achievement unlocked" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil]; [alert show]; } }

    Read the article

  • How to debug KVO

    - by user8472
    In my program I use KVO manually to observe changes to values of object properties. I receive an EXC_BAD_ACCESS signal at the following line of code inside a custom setter: [self willChangeValueForKey:@"mykey"]; The weird thing is that this happens when a factory method calls the custom setter and there should not be any observers around. I do not know how to debug this situation. Update: The way to list all registered observers is observationInfo. It turned out that there was indeed an object listed that points to an invalid address. However, I have no idea at all how it got there. Update 2: Apparently, the same object and method callback can be registered several times for a given object - resulting in identical entries in the observed object's observationInfo. When removing the registration only one of these entries is removed. This behavior is a little counter-intuitive (and it certainly is a bug in my program to add multiple entries at all), but this does not explain how spurious observers can mysteriously show up in freshly allocated objects (unless there is some caching/reuse going on that I am unaware of). Modified question: How can I figure out WHERE and WHEN an object got registered as an observer? Update 3: Specific sample code. ContentObj is a class that has a dictionary as a property named mykey. It overrides: + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey { BOOL automatic = NO; if ([theKey isEqualToString:@"mykey"]) { automatic = NO; } else { automatic=[super automaticallyNotifiesObserversForKey:theKey]; } return automatic; } A couple of properties have getters and setters as follows: - (CGFloat)value { return [[[self mykey] objectForKey:@"value"] floatValue]; } - (void)setValue:(CGFloat)aValue { [self willChangeValueForKey:@"mykey"]; [[self mykey] setObject:[NSNumber numberWithFloat:aValue] forKey:@"value"]; [self didChangeValueForKey:@"mykey"]; } The container class has a property contents of class NSMutableArray which holds instances of class ContentObj. It has a couple of methods that manually handle registrations: + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey { BOOL automatic = NO; if ([theKey isEqualToString:@"contents"]) { automatic = NO; } else { automatic=[super automaticallyNotifiesObserversForKey:theKey]; } return automatic; } - (void)observeContent:(ContentObj *)cObj { [cObj addObserver:self forKeyPath:@"mykey" options:0 context:NULL]; } - (void)removeObserveContent:(ContentObj *)cObj { [cObj removeObserver:self forKeyPath:@"mykey"]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (([keyPath isEqualToString:@"mykey"]) && ([object isKindOfClass:[ContentObj class]])) { [self willChangeValueForKey:@"contents"]; [self didChangeValueForKey:@"contents"]; } } There are several methods in the container class that modify contents. They look as follows: - (void)addContent:(ContentObj *)cObj { [self willChangeValueForKey:@"contents"]; [self observeDatum:cObj]; [[self contents] addObject:cObj]; [self didChangeValueForKey:@"contents"]; } And a couple of others that provide similar functionality to the array. They all work by adding/removing themselves as observers. Obviously, anything that results in multiple registrations is a bug and could sit somewhere hidden in these methods. My question targets strategies on how to debug this kind of situation. Alternatively, please feel free to provide an alternative strategy for implementing this kind of notification/observer pattern.

    Read the article

  • KVO on the "windows" value of UIApplication?

    - by Ariel Malka
    The following is not working: [[UIApplication sharedApplication] addObserver:self forKeyPath:@"windows" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL]; Together with that, on the Observer side: - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"never reached!"); } Any clues? N.B. My uber-goal is to get a notification when a (system-generated) UIAlertView is shown.

    Read the article

  • using KVO to filter an NSTableView using NSPredicate (with arrays)

    - by KingRufus
    My UI is not updating when I expect it to. The application displays "projects" using a view similar to iTunes -- a source list on the left lets you filter a list (NSTableView) on the right. My filters update properly when they are examining any simple field (like name, a string), but not for arrays (like tags). I'm removing a tag from one of my objects (from an NSMutableArray field called "tags") and I expect it to disappear from the list because it no longer matches the predicate that is bound to my table's NSArrayController. ProjectBrowser.mm: self.filter = NSPredicate* srcPredicate = [NSPredicate predicateWithFormat:@"%@ IN %K", selectedTag, @"tags"]; Project.mm: [self willChangeValueForKey:@"tags"]; [tags removeAllObjects]; [self didChangeValueForKey:@"tags"]; I've also tried this, but the result is the same: [[self mutableArrayValueForKey:@"tags"] removeAllObjects]; Interface Builder setup: a ProjectBrowser object is the XIB's File Owner an NSArrayController (Project Controller) has its Content Array bound to "File's Owner".projects Project Controller's filter predicate is bound to "File's Owner".filter NSTableView's column is bound to "Project Controller".name

    Read the article

  • using KVO to update an NSTableView filtered by an NSPredicate

    - by KingRufus
    My UI is not updating when I expect it to. The application displays "projects" using a view similar to iTunes -- a source list on the left lets you filter a list (NSTableView) on the right. My filters update properly when they are examining any simple field (like name, a string), but not for arrays (like tags). I'm removing a tag from one of my objects (from an NSMutableArray field called "tags") and I expect it to disappear from the list because it no longer matches the predicate that is bound to my table's NSArrayController. ProjectBrowser.mm: self.filter = NSPredicate* srcPredicate = [NSPredicate predicateWithFormat:@"%@ IN %K", selectedTag, @"tags"]; Project.mm: [self willChangeValueForKey:@"tags"]; [tags removeAllObjects]; [self didChangeValueForKey:@"tags"]; I've also tried this, but the result is the same: [[self mutableArrayValueForKey:@"tags"] removeAllObjects]; Interface Builder setup: a ProjectBrowser object is the XIB's File Owner an NSArrayController (Project Controller) has its Content Array bound to "File's Owner".projects Project Controller's filter predicate is bound to "File's Owner".filter NSTableView's column is bound to "Project Controller".name

    Read the article

  • How listen for UIButton state change?

    - by morticae
    I'm extending UIButton with generic functionality to change certain appearance attributes based on the displayed title. In order to do this, I need to detect and respond to changes in the "state" property. This is so I make sure the appearance is adjusted properly if the user has set different titles for different states. I assumed I would need to use some sort of KVO like the following: [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil]; But this does not seem to fire the observeValueForKeyPath:... method for @"state" or @"currentTitle". I assume this is because UIButton does not implement the KVO pattern for those properties. I do not want to just listen for clicks. Those events cause a state change, but are not the only potential causes. Does anyone know a way to listen to and respond to state changes of a UIButton? Thanks

    Read the article

  • Observing model changes with Cocoa Bindings and NSArrayController

    - by jbrennan
    I've got an NSArrayController bound to a mutable array in my controller, which manages an array of my model objects. The array controller is bound to my UI. It works well. Now I'm trying to manually observe when a value changes in my model in my controller class (basically I'm marking the changed model as "needsToSave" for later on, but there are a few other tasks I have when it changes). I've read up on KVO but I'm not entirely sure what I need to be observing... The NSArrayController? The array of objects? each model object itself? Confusion. Any pointers would be very helpful. Thanks in advance!

    Read the article

  • how to udate window controls(NSTextField,NSCheckbox and etc) in binding manually

    - by Amit
    Hi, I am working on an application in which i need to store all the NSObject subclass properties into plist file and then allow users to store it and restore it. We call it profile and it can restore the saved state of all the controls/views on the window in my application. I have completed the storing/Restoring part, but the issue is when i am updating the class properties manually, it is not updating the control state Like checkboxs and others which is bind with the class property. Please let me know how can i update the controls state, if its KVC/KVO updated programatically. Thanks in advance

    Read the article

  • Updates to NSDictionary attribute in CoreData not saving

    - by sfkaos
    I have created an Entity in CoreData that includes a Transformable attribute type implemented as an NSDictionary. The NSDictionary attribute only contains values of a custom class. The properties of the custom class are all of type NSString. The custom class complies with NSCoding implementing: -(void)encodeWithCoder:(NSCoder*)coder; -(id)initWithCoder:(NSCoder *)coder When saving the Entity for the first time all attributes including the Transformable (NSDictionary) type are properly saved in the DB. When the same Entity is fetched from the DB and updated (including the Transformable attribute) it seems to be updated properly. However, when the app is closed and then reopened fetching the Entity does not show the updated Transformable attribute-type though the rest of the attributes of type NSDate and NSString are up-to-date. The Transformable attribute is the original saved value not the updated value. Is this a problem with KVO or am I missing something else when trying to save an NSDictionary filled with a custom class to CoreData?

    Read the article

  • Difference between mutableArrayValueForKey and calling insertObject:inEmployeesAtIndex: directly

    - by jasonbogd
    I have a question regarding using KVO-compliant methods to insert/remove objects from an array. I'm working through Aaron Hillegass' Cocoa Programming for Mac OS X and I saw the following line of code (in the insertObject:inEmployeesAtIndex: method: [[undoManager prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index]; Correct me if I'm wrong, but I always thought it was better to call mutableArrayValueForKey: and then removeObjectAtIndex:...so I tried changing the above line to this: [[undoManager prepareWithInvocationTarget:[self mutableArrayValueForKey:@"employees"]] removeObjectAtIndex:index]; And it didn't work. Can someone explain the difference and why the first line works but the second line doesn't?

    Read the article

  • Best way to handle enable/disable of UIButtons on iPhone

    - by Anonymous
    I have an increase button and a decrease button, both of which affect a variable. The variable has a minimum and the decrease button will be disabled once that minimum is reached. Likewise for the maximum value of the variable. In my controller, I have two IBActions for both the increase and decrease actions, and two IBOutlets, in order to disable the buttons accordingly. At the bottom of my KVO statement to handle the value change, I have: increaseButton.enabled = value != MAX; decreaseButton.enabled = value != MIN; While this code is functional, I can't help but feel that feel this is a naive approach to the problem, especially since this requires 4 connections. Is there a better solution to this, one that uses fewer connections?

    Read the article

  • NSArraycontroller selectionIndexes bindings

    - by Michael Scherbaum
    Hi all, I have the following set-up: A Window that has a splitView in which I display I NSCollectionView in the left view and a detailView in the right view. Both views are set-up in separate xibs. Furthermore I have a Datacontroller (of class NSArrayController) that manages a mutable Array of NSMutableDictionaries (moviesForChoice). The dataController is set-up as application delegate. The movie objects in the array have properties like (name, plot, genre etc.) so far so good... In the xib for the NScollectionview I bound a NSArraycontroller content property to my datacontroller via Application.delegate.moviesForChoice The collectionView accesses the arraycontroller.arrrangedObjects and arraycontroller.selectionIndexes. This works fine the contents are displayed and the selection works fine in the collectionview (my collectionviewItem renders a selection color) In the xib for the detailView I want to display information for the selected object in the collectionview. Therefore I also added an arraycontroller to the xib, bound the content aray to Application.delegate.moviesForChoice and bound the NSTextfields in the view to e.g. arraycontroller.selection.name Here comes my issue: everytime I open the window with the two xibs, my collectionview displays all movies that are for choice correctly, and the detailview displays the information for the 1st object in my collectionview. Whenever I click on a different movie in the collectionView the res. item renders a selection color, but the detailView doesn't update. My understanding of it would be that the DataController is not informed about updates in the selectionIndexes and can therefore not trigger an update in the detailView. Correct me if I'm wrong... To remedy this I tried to bind the selectionIndexes property of the arraycontroller in the collectionView xib to Application.delegate.moviesForChoice.selecionIndexes but this failed with: addObserver:forKeyPath:options:context:] is not supported. Key path: selectionIndexes I could imagine that this means that the datacontroller is not KVO compliant for my Array moviesForChoice, but I implemented the following methods for it: -(void)insertObject:(NSDictionary *)dict inMoviesForChoiceAtIndex:(NSUInteger)index { [moviesForChoice insertObject:dict atIndex:index]; } -(void)removeObjectFromMoviesForChoiceAtIndex:(NSUInteger)index { [moviesForChoice removeObjectAtIndex:index]; } -(void)setMoviesForChoice:(NSMutableArray *)a { moviesForChoice = a; } -(NSArray*)moviesForChoice { return moviesForChoice; } -(NSUInteger)countOfMoviesForChoice { return [moviesForChoice count]; } - (void)addMovieForChoiceObject:(Movie *)anObject { [moviesForChoice addObject:anObject]; } So where am I wrong? How do I correctly bind to the selectionIndexes? You help is much appreciated! M

    Read the article

  • How to properly deal with KVO notifications when an managed object turns into a fault?

    - by dontWatchMyProfile
    From the docs: When Core Data turns an object into a fault, key-value observing (KVO) change notifications (see Key-Value Observing Programming Guide) are sent for the object’s properties. If you are observing properties of an object that is turned into a fault and the fault is subsequently realized, you receive change notifications for properties whose values have not in fact changed. So if an object turns into a fault, Core Data does send KVO notifications for changed properties? So I must always check for isFault == NO before beeing happy about the notification?

    Read the article

  • How can I write cocoa bindings as code instead of in the Interface Builder?

    - by johnjohndoe
    In my modell got an NSMutableArray that keeps track of a changing number of elements. In my view I got a NSTextField that shows the number of elements. The view gots unarchived from the nib file and alloc/inits the modell. Therefore it knowns about the modell and the contained array. I established the connection as follows. In the Interface Builder at the textfield I added a Cocoa Binding "path" like this: myModell.myArray.@count. By this I can access the count property (which is a must since the array itself does not change). The binding is based on key-value compliance which I established at the modell so the array can be accessed. But key-value compliance is not part of the questions. My question: How can I put the binding into the sourcecode and not writing it into the Interface Builder?

    Read the article

  • iPhone OS: Is there a way to set up KVO between two ManagedObject Entities?

    - by nickthedude
    I have 2 entities I want to link with KVO, one a single statTracker class that keeps track of different stats and the other an achievement class that contains information about achievements. Ideally what I want to be able to do is set up KVO by having an instance of the achievement class observe a value on the statTracker class and also set up a threshold value at which the achievement instance should be "triggered"(triggering in this case would mean showing a UIAlertView and changing a property on the achievement class.) I'd like to also set these relationships up on instantiation of the achievement class if possible so kind of like this: Achievement *achievement1 = (Achievement *)[NSEntityDescription insertNewObjectForEntityForName:@"Achievement" inManagedObjectContext:[[CoreDataSingleton sharedCoreDataSingleton] managedObjectContext]]; [achievement1 setAchievementName:@"2 time launcher"]; [achievement1 setAchievementDescription:@"So you've decided to come back for more eh? Here are some achievement points to get you going"]; [achievement1 setAchievementPoints:[NSNumber numberWithInt:300]; [achievement1 setObjectToObserve:@"statTrackerInstace" propertyToObserve:@"timesLaunched" valueOfPropertToSatisfyAchievement:2] Anyone out there know how I would set this up? Is there some way I could do this by way of relationships that I'm not seeing? Thanks, Nick

    Read the article

  • Key Coder/Observer example for Iphone

    - by ReduxDJ
    I'm trying to implement KVO into an application, yet, I've followed the documentation provided by Apple, however I can't get it to work. I'm hoping to see a bare minimal example of how to use this with my NSObjects. My use case, is I want one item in a table-cell to update without loading the entire data in a tableView because I am loading images from URLs and I don't want to reload all of the image, while I am polling a server. Thanks,

    Read the article

  • Detecting a Lightweight Core Data Migration

    - by hadronzoo
    I'm using Core Data's automatic lightweight migration successfully. However, when a particular entity gets created during a migration, I'd like to populate it with some data. Of course I could check if the entity is empty every time the application starts, but this seems inefficient when Core Data has a migration framework. Is it possible to detect when a lightweight migration occurs (possibly using KVO or notifications), or does this require implementing standard migrations? I've tried using the NSPersistentStoreCoordinatorStoresDidChangeNotification, but it doesn't fire when migrations occur.

    Read the article

  • objective c id *

    - by joels
    I am using the KVO validations in my cocoa app. I have a method - (BOOL)validateName:(id *)ioValue error:(NSError **)outError My controls can now have their bindings validate. How do I invoke that method with a id * NOT id To use the value that is passed in (a pointer to a string pointer) I call this: NSString * newName = (NSString *)*ioValue; if ([newName length] < 4) { otherwise i get bad exec crashes... passing in with type casting doesnt work: (id *)myStringVar passing in with a regular id doesnt work either : (id) myStringVar

    Read the article

1 2  | Next Page >