issue is running and stopping CFRunLoop - c++

i have situation where i need to wait till one block is get completed and then only move forward with my code for that i use CFRunLooprun and stop this is how do it i will explain more things in comment in my code
[self fatchAllEvent]; // BLOCK IS IN THIS METHOD
NSLog(#"loop will start");
CFRunLoopRun();
NSLog(#"LOOP IS STOOPED");
-(void)fatchAllEvent{
events = [[NSMutableArray alloc]init];
// // Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
// __block typeof (self) weakSelf = self; // replace __block with __weak if you are using ARC
dispatch_async(dispatch_get_main_queue(), ^{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
[events removeAllObjects];
NSLog(#" granted");
NSLog(#"User has granted permission!");
// Create the start date components
NSDateComponents *twoYearAgoComponents = [[NSDateComponents alloc] init];
twoYearAgoComponents.year = -2;
NSDate *oneDayAgo = [calendar dateByAddingComponents:twoYearAgoComponents
toDate:[NSDate date]
options:0];
// Create the end date components
NSDateComponents *twoYearFromNowComponents = [[NSDateComponents alloc] init];
twoYearFromNowComponents.year = 2;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:twoYearFromNowComponents
toDate:[NSDate date]
options:0];
// Create the predicate from the event store's instance method
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneDayAgo
endDate:oneYearFromNow
calendars:nil];
// Fetch all events that match the predicate
events =(NSMutableArray*) [eventStore eventsMatchingPredicate:predicate];
NSLog(#"The content of array is%#",events);
}
else
{
NSLog(#"Not granted");
}
NSLog(#"LOOP WILL STOP"); // THIS GETS PRINT
CFRunLoopStop(CFRunLoopGetCurrent()); // BUT LOOP IS NOT STOPPING HERE SO MY APP JUST GET HANGED ;
}];
});
}
else
{
[events removeAllObjects];
NSLog(#"Autometiclly granted permission!");
// Create the start date components
NSDateComponents *twoYearAgoComponents = [[NSDateComponents alloc] init];
twoYearAgoComponents.year = -2;
NSDate *oneDayAgo = [calendar dateByAddingComponents:twoYearAgoComponents
toDate:[NSDate date]
options:0];
// Create the end date components
NSDateComponents *twoYearFromNowComponents = [[NSDateComponents alloc] init];
twoYearFromNowComponents.year = 2;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:twoYearFromNowComponents
toDate:[NSDate date]
options:0];
// Create the predicate from the event store's instance method
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneDayAgo
endDate:oneYearFromNow
calendars:nil];
// Fetch all events that match the predicate
events =(NSMutableArray*) [eventStore eventsMatchingPredicate:predicate];
NSLog(#"The content of array is%#",events);
}
}

You can't do that this way - you have to call your function, and then in the completion block of the async dispatch, at the end, call a function that continues with what you want to do.
In your above code you mix up asynchronous programming with synchronous execution, that won't work.

Related

ARC Objective-C delegation through a C++ abstract layer

I'm programming simple cross platform C++ layer (dylib) that gets implemented by Objective-C. The parts that are platform specific are included through platform macros.
The NSUserNotificationCenter requires the delegation pattern for handling specific actions, clicking on the notification for example. The issue I'm facing is that as soon as I execute send, the notification is sent but the instance unloads right after that. Thus the onclick notification action never gets called (didActivateNotification) instead it crashes for a bad pointer. How can I make this work?
Note:
SomeObjectiveC.mm is a class located in my Application. AppleUserNotificationManager.m is initialized by NotificationManager+apple.mm and both are located in my Dylib.
SomeObjectiveC.mm
Notification *notification = new Notification;
notification->setTitle("Foo bar notification");
notification->setMessage("Hello world!");
NotificationManager *notificationManager = new NotificationManager;
notificationManager->send(notification);
NotificationManager+apple.mm
#include "notificationManager+apple.hpp"
bool NotificationManager::send(Notification *notification)
{
AppleUserNotificationManager *notificationManager = [[AppleUserNotificationManager alloc] init];
NSString *title = [NSString stringWithUTF8String:notification->getTitle().c_str()];
NSString *message = [NSString stringWithUTF8String:notification->getMessage().c_str()];
if (notification->getSoundName().empty()) {
[notificationManager sendWithTitle:title andMessage:message];
}
NSString *soundName = [NSString stringWithUTF8String:notification->getSoundName().c_str()];
[notificationManager sendWithTitle:title andMessage:message andSound: soundName];
return true;
}
AppleUserNotificationManager.m
#import "AppleUserNotificationManager.h"
#implementation AppleUserNotificationManager
#synthesize userNotification;
- (id)init
{
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self];
userNotification = [[NSUserNotification alloc] init];
self = [super init];
return self;
}
/**
* #param NSUserNotificationCenter center
* #param NSUserNotification notification
*
* #return bool
*/
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
return YES;
}
/**
* #param NSUserNotificationCenter center
* #param NSUserNotification notification
*/
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSString *notificationText = [notification informativeText];
NSString *urlRegEx = #"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", urlRegEx];
if ([urlTest evaluateWithObject:notificationText]) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:notificationText]];
}
}
/**
* #param NSString title
* #param NSString message
* #param NSString soundName
*/
- (void)sendWithTitle:(NSString *)title andMessage:(NSString *)message andSound:(NSString *)soundName{
userNotification.title = title;
userNotification.informativeText = message;
userNotification.soundName = soundName;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: userNotification];
}
#end
Used self pointer before allocated?
Could below change fix the problem?
- (id)init
{
self = [super init];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate: self];
userNotification = [[NSUserNotification alloc] init];
return self;
}

PKPass files getting replaced on Passbook for files in the same app

My App deals with downloading coupons & save into Passbook. But each time I download a different coupon, file is getting replaced on Passbook.
Below given is my code to add my coupons to Passbook :
Step 1 : Added 'PassKit' framework to the project & imported the same.
Step 2 : Added 'PKAddPassesViewControllerDelegate' on my h file.
Step 3 :
- (void) generatePass {
if (![PKPassLibrary isPassLibraryAvailable]) {
[[[UIAlertView alloc] initWithTitle:#"Error"
message:#"PassKit not available"
delegate:nil
cancelButtonTitle:#"Pitty"
otherButtonTitles: nil] show];
return;
}
else {
NSData *passData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://(url).pkpass"]];
NSError* error = nil;
PKPass *newPass = [[PKPass alloc] initWithData:passData
error:&error];
if (error!=nil) {
[[[UIAlertView alloc] initWithTitle:#"Passes error"
message:[error
localizedDescription]
delegate:nil
cancelButtonTitle:#"Ooops"
otherButtonTitles: nil] show];
return;
}
PKAddPassesViewController *addController =
[[PKAddPassesViewController alloc] initWithPass:newPass];
addController.delegate = self;
[self presentViewController:addController
animated:YES
completion:nil];
}
}
Passbook indexes passes by serialNumber and passTypeIdentifier. When adding a pass, if a pass with a matching serialNumber and passTypeIdentifier already exists in a user's pass library, that pass will be overwritten by the pass being added.
To add multiple passes for the same passTypeIdentifer you will have to generate a unique serialNumber for each new pass.

fast enumeration and selecting objects

I've an array (tempList) populated with records read from a sqlite data source. I want to sort these records based on 'region'. So I've set up the following NSMUtableArrays: _Asia, _Africa, _CentralAmerica, _southAmerica.
The method compiles without any errors. Where the 'if' statements return true, it executes the 'addObject' but the element is not added to the array.
- (void) sortIntoRegions: tempList
{
for (beans *arrayElement in tempList) {
NSLog(#"region: %#", arrayElement.region);
if ([arrayElement.region isEqualToString:#"Africa"]) {
[_africa addObject:arrayElement.name];
} else if ([arrayElement.region isEqualToString: #"Asia & South Pacific"]) {
[_asia addObject:arrayElement.name];
}
else if ([arrayElement.region isEqualToString: #"Central America"]) {
[_centralAmerica addObject:arrayElement];
} else if ([arrayElement.region isEqualToString: #"South America"]) {
[_southAmerica addObject:arrayElement];
}
}
}
I'm going to go out on a limb and guess that you have declared _africa, _asia, _centralAmerica, and _southAmerica to be NSMutableArray *, but you have not initialized them. That is, you have not written any code like this:
_africa = [[NSMutableArray alloc] init];
_asia = [[NSMutableArray alloc] init];
_centralAmerica = [[NSMutableArray alloc] init];
_southAmerica = [[NSMutableArray alloc] init];
Your instance variables are set to nil when your object is created. Sending a message (like addObject:) to nil has no effect (and doesn't print a warning or error message).

Synchronize IOS core data with web service?

Here is my problem:
I want to use core data - speed and connectivity issues to build my IOS app. The data stored in core data is coming from a SQLServer database which I can access through a yet-to-be-defined web service.
Any changes to the data stored in core data needs to be synchronized with the SQLServer via a web service. In addition, I need to buffer changes that don't get synchronized because of connectivity issues.
I also need to update core data with any changes that have occured on the server. This could happen on a schedule set in user preferences.
Solutions I've Explored:
Using NSIncrementalStore class (new in IOS 5). I'm very confused on what this does exactly but it sounds promising. From what I can tell, you subclass NSIncrementalStore which allows you to intercept the regular core data API calls. I could then pass on the the information to core data as well as sync it with the external database via a web service. I could be completely wrong. But assuming I'm right, how would I sync deltas if the connection to the internet is down?
AFIncrementalStore - This is a subclass off of NSIncrementalStore using AFNetworking to do the web services piece.
RestKit - I'm a little concerned on how active this API is and it seems to be going through a transition to block functionality. Has anyone used this extensively?
I'm leaning towards AFIncrementalStore since this is using (what seems to be) a more standard approach. The problem is, I could be completely off on what NSIncrementalStore really is.
A link to some sample code or tutorial would be great!
My solution to this was to store two copies of the data set in a CoreData database. One represents the last-known server state and is immutable. The other is edited by the user.
When it is time to sync changes, the app creates a diff between the edited and immutable copies of the data. The app sends the diff to a web service which applies the diff to its own copy of the data. It replies with a full copy of the data set, which the app overwrites onto both of its copies of the data.
The advantages are:
If there is no network connectivity, no changes are lost: the diff is calculated each time the data set needs to be sent, and the immutable copy is only changed on a successful sync.
Only the minimum amount of information that needs to be sent is transmitted.
Multiple people can edit the same data at the same time without using locking strategies with a minimum opportunity for data loss via overwrites.
The disadvantages are:
Writing the diffing code is complex.
Writing the merging service is complex.
Unless you are a metaprogramming guru, you'll find that your diff/merge code is brittle and has to change whenever you change your object model.
Here are some of the considerations I had when coming up with the strategy:
If you allow changes to be made offline, checkin/checkout locking won't work (how can you establish a lock with no connection?).
What happens if two people edit the same data at the same time?
What happens if one person edits data on one iOS device when connectionless, switches it off, edits on another device and then turns the original device back on?
Multithreading with CoreData is an entire problem class in itself.
The closest thing I've heard of to out-of-the-box support to do anything remotely like this is the new iCloud/CoreData syncing system in iOS6, which automatically transmits entities from a CoreData database to iCloud when they change. However, that means you have to use iCloud.
EDIT: This is very late, I know, but here's a class that is capable of producing a diff between two NSManagedObject instances.
// SZManagedObjectDiff.h
#interface SZManagedObjectDiff
- (NSDictionary *)diffNewObject:(NSManagedObject *)newObject withOldObject:(NSManagedObject *)oldObject
#end
// SZManagedObjectDiff.m
#import "SZManagedObjectDiff.h"
#implementation SZManagedObjectDiff
- (NSDictionary *)diffNewObject:(NSManagedObject *)newObject withOldObject:(NSManagedObject *)oldObject {
NSDictionary *attributeDiff = [self diffAttributesOfNewObject:newObject withOldObject:oldObject];
NSDictionary *relationshipsDiff = [self diffRelationshipsOfNewObject:newObject withOldObject:oldObject];
NSMutableDictionary *diff = [NSMutableDictionary dictionary];
if (attributeDiff.count > 0) {
diff[#"attributes"] = attributeDiff;
}
if (relationshipsDiff.count > 0) {
diff[#"relationships"] = relationshipsDiff;
}
if (diff.count > 0) {
diff[#"entityName"] = newObject ? newObject.entity.name : oldObject.entity.name;
NSString *idAttributeName = newObject ? newObject.entity.userInfo[#"id"] : oldObject.entity.userInfo[#"id"];
if (idAttributeName) {
id itemId = newObject ? [newObject valueForKey:idAttributeName] : [oldObject valueForKey:idAttributeName];
if (itemId) {
diff[idAttributeName] = itemId;
}
}
}
return diff;
}
- (NSDictionary *)diffRelationshipsOfNewObject:(NSManagedObject *)newObject withOldObject:(NSManagedObject *)oldObject {
NSMutableDictionary *diff = [NSMutableDictionary dictionary];
NSDictionary *relationships = newObject == nil ? [[oldObject entity] relationshipsByName] : [[newObject entity] relationshipsByName];
for (NSString *name in relationships) {
NSRelationshipDescription *relationship = relationships[name];
if (relationship.deleteRule != NSCascadeDeleteRule) continue;
SEL selector = NSSelectorFromString(name);
id newValue = nil;
id oldValue = nil;
if (newObject != nil && [newObject respondsToSelector:selector]) newValue = [newObject performSelector:selector];
if (oldObject != nil && [oldObject respondsToSelector:selector]) oldValue = [oldObject performSelector:selector];
if (relationship.isToMany) {
NSArray *changes = [self diffNewSet:newValue withOldSet:oldValue];
if (changes.count > 0) {
diff[name] = changes;
}
} else {
NSDictionary *relationshipDiff = [self diffNewObject:newValue withOldObject:oldValue];
if (relationshipDiff.count > 0) {
diff[name] = relationshipDiff;
}
}
}
return diff;
}
- (NSDictionary *)diffAttributesOfNewObject:(NSManagedObject *)newObject withOldObject:(NSManagedObject *)oldObject {
NSMutableDictionary *diff = [NSMutableDictionary dictionary];
NSArray *attributeNames = newObject == nil ? [[[oldObject entity] attributesByName] allKeys] : [[[newObject entity] attributesByName] allKeys];
for (NSString *name in attributeNames) {
SEL selector = NSSelectorFromString(name);
id newValue = nil;
id oldValue = nil;
if (newObject != nil && [newObject respondsToSelector:selector]) newValue = [newObject performSelector:selector];
if (oldObject != nil && [oldObject respondsToSelector:selector]) oldValue = [oldObject performSelector:selector];
newValue = newValue ? newValue : [NSNull null];
oldValue = oldValue ? oldValue : [NSNull null];
if (![newValue isEqual:oldValue]) {
diff[name] = #{ #"new": newValue, #"old": oldValue };
}
}
return diff;
}
- (NSArray *)diffNewSet:(NSSet *)newSet withOldSet:(NSSet *)oldSet {
NSMutableArray *changes = [NSMutableArray array];
// Find all items that have been newly created or updated.
for (NSManagedObject *newItem in newSet) {
NSString *idAttributeName = newItem.entity.userInfo[#"id"];
NSAssert(idAttributeName, #"Entities must have an id property set in their user info.");
id newItemId = [newItem valueForKey:idAttributeName];
NSManagedObject *oldItem = nil;
for (NSManagedObject *setItem in oldSet) {
id setItemId = [setItem valueForKey:idAttributeName];
if ([setItemId isEqual:newItemId]) {
oldItem = setItem;
break;
}
}
NSDictionary *diff = [self diffNewObject:newItem withOldObject:oldItem];
if (diff.count > 0) {
[changes addObject:diff];
}
}
// Find all items that have been deleted.
for (NSManagedObject *oldItem in oldSet) {
NSString *idAttributeName = oldItem.entity.userInfo[#"id"];
NSAssert(idAttributeName, #"Entities must have an id property set in their user info.");
id oldItemId = [oldItem valueForKey:idAttributeName];
NSManagedObject *newItem = nil;
for (NSManagedObject *setItem in newSet) {
id setItemId = [setItem valueForKey:idAttributeName];
if ([setItemId isEqual:oldItemId]) {
newItem = setItem;
break;
}
}
if (!newItem) {
NSDictionary *diff = [self diffNewObject:newItem withOldObject:oldItem];
if (diff.count > 0) {
[changes addObject:diff];
}
}
}
return changes;
}
#end
There's more information about what it does, how it does it and its limitations/assumptions here:
http://simianzombie.com/?p=2379
Use the Parse platform and its IOS SDK to structure and store info. It can cache data locally so you can retrieve it quickly and when there is no connectivity.

Passing an NSArray populated with NSDictionaries to a NSAppleScript

So I have an Apple Script that is running one of the functions of my program like so:
[ NSThread detachNewThreadSelector:#selector(runAppleScriptTask)
toTarget:self
withObject:nil];
Using this method:
-(void)runAppleScriptTask
{
mainBundle = [NSBundle bundleForClass:[self class]];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
NSString *scriptPath = [[NSBundle mainBundle] pathForResource: #"AttemptToRepair"
ofType: #"scpt"];
NSLog(#"Found AppleScript Path:%#",scriptPath);
// Run the Apple Script
NSAppleScript *scriptObject = [[NSAppleScript alloc]initWithContentsOfURL:[NSURL fileURLWithPath: scriptPath]
error:&errorDict];
returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
NSLog(#"Return Discriptor,%#",returnDescriptor);
NSString *returnValue = #"User Canceled";
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] init];
if ([ returnDescriptor stringValue]) {
returnValue = [ returnDescriptor stringValue];
[ returnDict setValue:returnValue forKey:#"returnValue"];
}
else {
if (errorDict) {
returnValue = [ returnDescriptor stringValue];
[ returnDict setValue:errorDict forKey:#"errorDict"];
}
}
NSLog(#"Found Return Value: %#",returnValue);
[scriptObject release];
// Notify
[[NSNotificationCenter defaultCenter]
postNotificationName:AttemptToRepairCompleteNotification
object:self
userInfo:returnDict];
[pool drain];
}
I have a NSArray (Full of Statuses) that I need to pass to the Apple Script. Right now I am dumping the file to a plist:
// File Drop the Global Status Array
BOOL gsaWroteSuccess = [ issueFile writeToFile:#"/private/tmp/gsa.plist" atomically:YES];
if (gsaWroteSuccess) {
NSLog(#"Wrote the current Global Status Array to file");
// Let objects know the Global Status is being updated
NSMutableDictionary *globalStatusUpdate = [[NSMutableDictionary alloc] init];
// Pass the mutated Data to our NSTable
[ globalStatusUpdate setValue:issueFile forKey:#"globalStatusArray"];
[[NSNotificationCenter defaultCenter]
postNotificationName:StatusUpdateNotification
object:self
userInfo:globalStatusUpdate];
}
else {
NSLog(#"Unable to write Global Status Array to file");
}
Which I can easily pick back up in the Apple Script via System Events plist infrastructure , but I would really rather do this all in RAM. Now I think I could use the property syntax mentioned here , http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html but I need this to work on 10.5,10.6 and 10.7 so I can't use anything that was not released yet. Any thoughts on a slick in memory based way to pass an NSArray full or NSDicitonary objects to my Apple Script (which will become a list in the Apple Script)?
Here is the Apple Script code for the file drop methodology right now if it helps
script AttemptToRepair
property parent : class "NSObject"
activate
set thePListPath to POSIX path of "/tmp/gsa.plist"
tell application "System Events"
set the plist_path to "/tmp/gsa.plist"
set the plist_file to property list file plist_path
set itemNodes to property list items of property list item "globalStatusArray" of plist_file
repeat with i from 1 to number of items in itemNodes
set itemNode to item i of itemNodes
set discription to value of property list item "discription" of itemNode
set metric to value of property list item "metric" of itemNode
set reason to value of property list item "reason" of itemNode
set status to value of property list item "status" of itemNode
display dialog "discription:" & discription & return & ¬
"metric:" & metric & return & ¬
"reason:" & reason & return & ¬
"status:" & status
end repeat
end tell
end script
run AttemptToRepair