Observing NSProgress - ios

I am using this code to track a changing number in NSProgress:
[progress addObserver:self forKeyPath:kProgressCancelledKeyPath options:NSKeyValueObservingOptionNew context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSProgress *progress = object;
NSLog(#"PROG: %f", progress.fractionCompleted);
// Check which KVO key change has fired
if ([keyPath isEqualToString:kProgressCancelledKeyPath]) {
// Notify the delegate that the progress was cancelled
}
else if ([keyPath isEqualToString:kProgressCompletedUnitCountKeyPath]) {
// Notify the delegate of our progress change
NSLog(#"PROG: %f", progress.fractionCompleted);
if (progress.completedUnitCount == progress.totalUnitCount) {
// Progress completed, notify delegate
NSLog(#"PROG: %f", progress.fractionCompleted);
}
}
}
This is what I get:
<NSProgress: phase=Loading; state=Waiting; fractionCompleted=0.000000> from peer: <MCPeerID: 0x14e1f220 DisplayName = alessandro's iPod touch-593288>
Noting more. I wanted to use a progress view with the fraction completed, but I don't get any value. The NSlog PROG is never called...

Related

observer gets called without changing "playbackLikelyToKeepUp" iOS

I am writing a custom video player and here is my doubt:
Even though the value for the key "playbackLikelyToKeepUp" doesn't change,observer method is getting called.Here is the screenshot below:
How is this possible?
Here is the code:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if([keyPath isEqualToString:#"playbackLikelyToKeepUp"]){
NSLog(#"%#",change[#"new"]);
if([change valueForKey:#"new"] == [NSNumber numberWithBool:YES])
[myPlayer play];
NSLog(#"%#",change);
}
}
and in the viewDidLoad method:
[myPlayerItem addObserver:self forKeyPath:#"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
and you can see the screenshot above.

Objective-C: Retrieving CGPoint from Key Value Observing

I'm monitoring the center of a view with the key value observer pattern, as follows:
- (void)viewDidLoad {
[[self.balanceView viewWithTag:BASE_TAG] addObserver:self
forKeyPath:#"center"
options:0
context:BASE_CONTEXT];
}
I'm trying to retrieve the value of the centerpoint and output it to NSLog, as follows:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (BASE_CONTEXT == context) {
CGPoint baseCenter = [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue];
NSLog(#"Base: (%f,%f)", baseCenter.x, baseCenter.y);
}
}
Whenever the UIView moves, an entry is output to NSLog. However, the centerpoint is always (0,0):
2014-07-18 20:48:51.210 Balance[8646:90b] Object: (0.000000,0.000000)
What is the correct way to retrieve the centerpoint?
options:0 gives you no change dictionary values -- if you want "new", you should use 1, or more appropriately, NSKeyValueObservingOptionNew.

iOS: Key-Value Observing does not dismiss modal view

I have the following key value observer method in a modal view:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"uploadComplete"]) {
NSLog(#"UploadVC hears upload complete");
[self dismissViewControllerAnimated:YES completion:nil];
}
}
I use this to watch a photo object and know when it is finished uploading. When I run this it behaves as expected, and the console logs "UploadVC hears upload complete" - but then the following line is not executed -- the modal does not get dismissed.
There are no errors or anything else, the view just sits there and the modal is never dismissed. What's going on here?
That may happen when you receive KVO notification on background thread and so attempts to update UI may result in any unexpected behaviour (UI not changed, changed after some delay, app crash etc etc). Make sure you call all updating UI code on main thread:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"uploadComplete"]) {
NSLog(#"UploadVC hears upload complete");
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}
}

Observing custom cell

I am trying to add observer (KVO) to observe my custom cell. Once the cell is selected I should receive a notification of the event.
My code:
[colMain addObserver:self forKeyPath:#"colMain" options:0 context:NULL];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (keyPath == #"colMain") {
NSLog(#"cell Selected");
[self performSelector:#selector(deleteCell) withObject:nil];
}
}
colMain stands for collectionView. I am not quite sure how to do it cause I don't have customCell as a property otherwise it does not compile.
Any ideas?
Why not just set a delegate on your collection view and then implement one of these two methods?
[– collectionView:shouldSelectItemAtIndexPath:]
[– collectionView:didSelectItemAtIndexPath:]

KVO on NSMutableString not working

I'm trying to observe changes to an NSMutableString isDetailView:
-(void)viewDidLoad {
[self addObserver:self forKeyPath:#"isDetailView" options:NSKeyValueObservingOptionNew context:nil];
[isDetailView setString:#"YES"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"obersedValueFOrKeyPath:%#", keyPath);
}
But the observeValueForKeyPath method never gets called. Any ideas?
You are not changing the property, only the content of the object it points to. If you make isDetailView a normal string and do
[self setIsDetailView: #"YES"]
it will work.
By the way, properties that start "is" are conventionally supposed to be boolean and that looks like a more appropriate type in this case too.

Resources