I am looking to integrate in my new app the option to sync core data in iCloud and so share the information on users devices. I looked around on the web but haven't found a good example or tutorial on how to do this with iOS7.
The last that I have done is to analyze the Apple receipt demo app and included in my app. It seams to work, at least at first view. Adding a record on one device and after a short while, the other device show the data - so far I was happy.
BUT, after restoring the app, the information was gone, on both devices. So i looked into the app (iExplorer) and have found the local Core Data and all my data is there. The next that I have observed is that the debugger shows this: (XXX) are of course not the real values :-)
2014-07-09 19:40:12.830 XXX[199:3507] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity: mobile~XXXXX:XXX
Using local storage: 1
2014-07-09 19:40:12.837 XXX[199:60b] asynchronously added persistent store!
2014-07-09 19:40:13.478 XXX[199:1803] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity: mobile~XXXXX:XXX
Using local storage: 0
What means first it seams like to use the local storage but than change to local storage 0.
this is the code used from Apple's demo app:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
// assign the PSC to our app delegate ivar before adding the persistent store in the background
// this leverages a behavior in Core Data where you can create NSManagedObjectContext and fetch requests
// even if the PSC has no stores. Fetch requests return empty arrays until the persistent store is added
// so it's possible to bring up the UI and then fill in the results later
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
// prep the store path and bundle stuff here since NSBundle isn't totally thread safe
NSPersistentStoreCoordinator* psc = persistentStoreCoordinator;
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:#"XXX.sqlite"];
// do this asynchronously since if this is the first time this particular device is syncing with preexisting
// iCloud content it may take a long long time to download
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
// this needs to match the entitlements and provisioning profile
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:#"XXXXX"];
cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];
// The API to turn on Core Data iCloud support here.
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:#"XXX", NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey, [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];
NSError *error = nil;
[psc lock];
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[psc unlock];
// tell the UI on the main thread we finally added the store and then
// post a custom notification to make your views do whatever they need to such as tell their
// NSFetchedResultsController to -performFetch again now there is a real store
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"asynchronously added persistent store!");
[[NSNotificationCenter defaultCenter] postNotificationName:#"RefetchAllDatabaseData" object:self userInfo:nil];
});
});
return persistentStoreCoordinator;
}
Could anyone help with tutorial or solution?
Try these sample apps for iOS and OSX.
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/
Related
I have an app already in the App Store that uses core data to save data.
Now, when iOS 8 is about to come out I wanna add a widget to it, thus I must use App Groups to share data between the binaries.
One problem though - I need to change the store location to support App Groups to all the existing users.
I wrote the following code, trying to move the store to the new path:
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *oldStoreURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
oldStoreURL = [oldStoreURL URLByAppendingPathComponent:#"Schooler.sqlite"];
NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:#"group.com.schooler.mycontainer"];
storeURL = [storeURL URLByAppendingPathComponent:#"Schooler.sqlite"];
if([[NSFileManager defaultManager] fileExistsAtPath:oldStoreURL.path] == YES && [[NSFileManager defaultManager] fileExistsAtPath:storeURL.path] == NO)
{
// Prior today extension - Need to move to new directory
NSError *error = nil;
if([[NSFileManager defaultManager] moveItemAtURL:oldStoreURL toURL:storeURL error:&error] == YES)
NSLog(#"Migrated successfully to new database location.");
else
NSLog(#"error: %#",error);
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
return _persistentStoreCoordinator;
}
The output is always "Migrated successfully to new database location.", although all the data that was saved on the app before has been deleted, As if it created a new database instead of just moving it.
What causes the problem? How should I fix it?
Thank you.
A Core Data NSSQLiteStoreType store created with the default options is actually several files, as described in Technical Q&A 1809: New default journaling mode for Core Data SQLite stores in iOS 7 and OS X Mavericks. This is important to remember when attempting to move a store outside of a migration process, and is the source of your issue - you are moving one file when you need to be moving all of them. Moving the files individually outside of Core Data and without the benefits of a file coordinator is not recommended, however. It's much better to use a migration instead.
A migration will take the data from the source store and migrate it to your new store location, essentially replicating the old data at the new location. The old data will still exist on the filesystem. In your application, you should perform the migration as you are now, but do not attempt to move the old data to the new location yourself - that is where things are going wrong.
Instead of moving files around yourself, you can rely on a migration to move the data for you. First, add a store to the persistent store coordinator with the URL of the source data. Then you will perform a migration to move that data to the new URL
NSPersistentStore *sourceStore = nil;
NSPersistentStore *destinationStore = nil;
NSDictionary *storeOptions = #{ NSSQLitePragmasOption : #{ #"journal_mode" :
#"WAL" } };
// Add the source store
if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:oldStoreURL options:storeOptions error:&error]){
// Handle the error
} else {
sourceStore = [coordinator persistentStoreForURL:oldStoreURL];
if (sourceStore != nil){
// Perform the migration
destinationStore = [coordinator migratePersistentStore:sourceStore toURL:storeURL options:storeOptions withType:NSSQLiteStoreType error:&error];
if (destinationStore == nil){
// Handle the migration error
} else {
// You can now remove the old data at oldStoreURL
// Note that you should do this using the NSFileCoordinator/NSFilePresenter APIs, and you should remove the other files
// described in QA1809 as well.
}
}
}
Once the migration has completed you can delete the old files. The example here explicitly specifies the SQLite journal options, this is to ensure that if the default options are changed in the future the code will still work. If you are using different options, you should use those instead.
In case having a version in Swift would be helpful:
let oldPersistentStoreURL: URL = ...
let sharedPersistentStoreURL: URL = ...
let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] // + any database-specific options
if FileManager.default.fileExists(atPath: oldPersistentStoreURL.path) {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
do {
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: oldPersistentStoreURL, options: options)
if let sourceStore = coordinator.persistentStore(for: oldPersistentStoreURL) {
let _ = try coordinator.migratePersistentStore(sourceStore, to: sharedPersistentStoreURL, options: options, withType: NSSQLiteStoreType)
// If migration was successful then delete the old files
}
} catch {
error.logErrors()
}
}
my use case is very simple. I need to
create a request NSManagedObject, ✓ works
pass it to e.g. postObject: method from Restkit, ✓ works
receive a response NSManagedObject in the completion block, ✓ works
process it and, ✓ works
delete both the request and the response objects using MR_deleteEntity, ✘ does not work
I'd like to use just MagicalRecord to create/delete/manage the entities.
The issue:
When I call the asynchronous save method from the MagicalRecord toolkit and after I exit the app I can still see the entities in the sqlite db file. After restart of the app new objects are added in the db without deleting a single instance although I explicitly call MR_deleteEntity on the objects. I admit the context management is something I yet have not grasped fully.
I am using the following setup to connect MagicalRecord with Restkit:
// 1. Setup the core data stack with the automigration
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:[GVUserDefaults standardUserDefaults].applicationStoreName];
// 2. Initialize managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];
// 3. create the persistentStoreManagedObjectContext and the mainQueueManagedObjectContext:
[managedObjectStore createManagedObjectContexts];
// 4. set the default and the root saving context:
[NSManagedObjectContext MR_defaultStoreCoordinator:managedObjectStore.mainQueueManagedObjectContext];
[NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext];
// 5. create RestKit manager:
self.rkManager = [TSNRKObjectManager managerWithBaseURL:[NSURL URLWithString:[self serverURL]]];
self.rkManager.requestSerializationMIMEType = RKMIMETypeJSON;
self.rkManager.managedObjectStore = managedObjectStore;
Question
I am trying to delete the request and the response objects this way:
[self saveWithBlock:^(NSManagedObjectContext *localContext) { // calls [MagicalRecord saveWithBlock:block completion:completion]
// some additional processing, getting data from the response
[loginResponse MR_deleteEntity];
[loginRequest MR_deleteEntity];
} completion:^(BOOL success, NSError *error) {
// some additional processing
}];
But I always get this message in the log:
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x110906470) NO CHANGES IN ** UNNAMED ** CONTEXT - NOT SAVING
I am using this page as reference https://gist.github.com/tonyarnold/4694673. The contexts for the creation of the request and the response entity are the same? What context is Restkit using for creation of the entities? Should I create the request entity also within the block in [MagicalRecord saveWithBlock:block completion:completion]? The example from https://github.com/blakewatters/RKMagicalRecord does not include the automigration setup and the asynchronous saving methods.
UPDATE:
Is this an acceptable solution? (I mean clean, graceful deletion):
[self.loginRequest MR_deleteEntity];
[self.loginRequest.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
// log
}];
[self.loginResponse MR_deleteEntity];
[self.loginResponse.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
// log
}];
I have tested this and it works.
UPDATE 2
In the completion block triggered from Restkit's postObject:... call, I can cleanup the entities from the persistent store this way:
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
[[self.loginRequest MR_inContext:localContext] MR_deleteEntity];
[[self.loginResponse MR_inContext:localContext] MR_deleteEntity];
} completion:^(BOOL success, NSError *error) {
if(success) {
self.loginRequest = nil;
self.loginResponse = nil;
// log, update ui
} else {
// log error
}
}];
You misunderstand the saving semantics for your multiple contexts. When you call saveWithBlock: you need to transfer all of your managed objects to that new local context that is created for you before you operate on them, otherwise the context has no changes and isn't saved. To transfer you need to get the managed object ids and find the existingObjectWithID:error:.
In this case, for 2 object deletion a you are better off deleting the objects directly from the main context (which is the one they belong to) and saving it up to the persistent store.
Does anyone make [FBDialogs canPresentShareDialogWithParams:nil] works properly ?
It's always returning me NO. What should I put in params ?
if ([FBDialogs canPresentShareDialogWithParams:nil]) {
NSURL* url = [NSURL URLWithString:#"http://www.google.fr"];
[FBDialogs presentShareDialogWithLink:url
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
NSLog(#"Error: %#", error.description);
} else {
NSLog(#"Success!");
}
}];
} else {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbComposer setInitialText:#"Google rocks !"];
[self presentViewController:fbComposer animated:YES completion:nil];
} else {
[[[UIAlertView alloc] initWithTitle:#"Informations" message:#"You have to be registered into the settings of your phone in order to share" delegate:nil cancelButtonTitle:#"Close" otherButtonTitles:nil] show];
}
}
According to the HelloFacebookSample from the SDK (and my own experience!):
FBShareDialogParams *p = [[FBShareDialogParams alloc] init];
p.link = [NSURL URLWithString:#"http://developers.facebook.com/ios"];
BOOL canShareFB = [FBDialogs canPresentShareDialogWithParams:p];
canShareFB will return YES if the Facebook app is installed in the system; returns NO if no Facebook app is found.
The fact is it always returns NO.
I think this the issue.
Make sure to pass a non-nil instance of FBShareDialogParams to the canPresentShareDialogWithParams method. The SDK expects to receive a valid instance of FBShareDialogParmas so the SDK can make sure that the version of the Facebook app on the device can actually open the content that's going to be shared.
For example, if FB adds support for sharing video via Share Dialog in a future version of the Facebook app on iOS, the canPresentShareDialogWithParams would return NO if an older version of the Facebook app is present on the device.
I can understand how the docs: https://developers.facebook.com/ios/share-dialog/ might be confusing here (apologies!). We'll update them to reflect this.
Thanks for the feedback; hope that helps!
I am updating my app to use the new Facebook SDK for iOS (3.0). However, I have run across an issue trying to use the feed dialog. I followed the instructions on Facebook's developer website regarding how to use the feed dialog with the new SDK, but I am getting this error when I show the dialog:
API Error Code: 110
API Error Description: Invalid user id
Error Message: Missing user cookie (to validate session user)
Here is my code:
Facebook *facebook = [[Facebook alloc] initWithAppId:FBSession.activeSession.appID andDelegate:nil];
facebook.accessToken = FBSession.activeSession.accessToken;
facebook.expirationDate = FBSession.activeSession.expirationDate;
NSMutableDictionary *feedParams = [[NSMutableDictionary alloc] init];
[feedParams setObject:self.video.alternateLink.href
forKey:#"link"];
// link title = video title
[feedParams setObject:self.video.title.stringValue
forKey:#"name"];
// link picture = video thumbnail
[feedParams setObject:self.video.mediaGroup.highQualityThumbnail.URLString
forKey:#"picture"];
NSDictionary *privacyDict = [NSDictionary dictionaryWithObjectsAndKeys:#"CUSTOM", #"value", #"SELF", #"friends", nil];
SBJSON *jsonWriter = [[SBJSON alloc] init];
[feedParams setObject:[jsonWriter stringWithObject:privacyDict error:NULL]
forKey:#"privacy"];
[jsonWriter release];
[facebook dialog:#"feed"
andParams:feedParams
andDelegate:self];
[feedParams release];
self.facebook = facebook;
[facebook release];
It seems like an authentication problem, but I am passing a valid access token to the Facebook object, so I'm not sure what the problem is. If anybody could help me, that would be great. Thanks.
You may use FBSession.activeSession when integrating with the legacy Facebook class, as you have shown. One possible gotcha when you use activeSession, rather than directly instantiating a session object, is that it may not be open. Here is a simple sample that shows the form for integrating the Facebook class with active session:
if (FBSession.activeSession.isOpen) {
// Create a Facebook instance
Facebook *facebook = [[Facebook alloc] initWithAppId:FBSession.activeSession.appID
andDelegate:nil]; // commonly self
// Set the session information for the Facebook instance
facebook.accessToken = FBSession.activeSession.accessToken;
facebook.expirationDate = FBSession.activeSession.expirationDate;
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I'm using the the Facebook SDK for iOS", #"name",
#"Facebook for iOS", #"caption",
#"Check out the Facebook SDK for iOS!", #"description",
#"https://developers.facebook.com/ios", #"link",
#"http://www.facebookmobileweb.com/hackbook/img/facebook_icon_large.png", #"picture",
nil];
// Invoke the dialog
[facebook dialog:#"feed" andParams:params andDelegate:nil];
[facebook release];
}
If the active session is not open, then you would get a failure along the lines of what you are seeing. A call along the lines of the following, somewhere earlier in your logic remedy this:
[FBSession openActiveSessionWithAllowLoginUI:YES];
Hope this helps!
I have an enterprise application that I want to keep running, so it can call a webservice and inform the user when there is something they need to do.
So, it now runs in the background, it makes the calls, gets results, but informing the user is my problem.
When I fire off a UILocalNotification the alert doesn't call my UIAlertDelegate, and I don't see how to set that alert to do that.
Here is my code for how I am doing the notification, and this actually brings up an alert, but I need it to then bring up a View so they can see the table, but the View it opens isn't one of the two my application uses.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%# has a message for you.", nil), #"FYR"];
localNotif.alertAction = NSLocalizedString(#"Read Msg", nil);
localNotif.soundName = nil;
localNotif.applicationIconBadgeNumber = -1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Your Background Task works", ItemListKey, #"Message from FYR", MessageTitleKey, nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
}
Also, so I tried to have an alert come up that I can set the delegate, so in my controller, where I call the above code I am also calling this:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"ShowAlert"
object:nil
userInfo:mydata];
This notification is then picked up and eventually this function is called, and this does work, but the alert isn't visible to the user, I expect because it is in the background.
- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title {
NSLog(#"%#", #"Attemping to show alert");
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title
message:pushmessage
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes",nil];
[alertView show];
if (alertView) {
[alertView release];
}
}
In the main delegate, I have this defined, and the appropriate functions:
#interface FYRViewAppDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate> {
my thought being that if I can get the alert that pops up to call this delegate then it can execute this code:
- (void)alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
alertview--;
if (buttonIndex == 0) {
} else {
[self.window addSubview:[tableController view]];
}
}
So, is there any way to get the UILocalNotification to use my delegate for the alert?
Or, is there a way to have an alert show up when the application is in the background?
Or, do I need to write two applications, so they communicate with remote notifications, so when the application is in the background one runs and then starts up the main application with a remote notification. I don't like this approach as it seems very messy, but would probably work.
Since the program never stops running
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
is only called at startup, the local notification never calls it and this also is never called:
- (void) application:(UIApplication*)application
didReceiveLocalNotification:(UILocalNotification *)localNotification {.
I suppose that you don't post notifications when the application is active. Couldn't you simply set a flag when you post a local notification and then check the flag when the application becomes active:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (localNotificationPosted) {
// Do whatever you need to do.
}
}
This will likely be the correct behavior, since the application will become active unless the user dismisses the notification. Even if the user dismisses the notification, showing new messages the next time the app is opened probably isn't a bad thing.