How to save a UIImageView to a CoreData Database - core-data

What is the SIMPLEST way to save a UIImageView to a CoreData Database. I have tried this:
Save:
UIImage *image = imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
[newContact setValue:imageData forKey:#"imageViewFinal"];
Retrive:
imageView.image = [matches valueForKey:#"imageViewFinal"];
and I have added an Attribute called 'imageViewFinal' with a Binary Data type.
PROBLEM:
When I go to build it and click the save button, the app crashes, what's the problem?
Thanks, Seb.

What #ShermanLo said is right, the crash log shows that you've modified your model but did not handle the conflict between the old & new store model versions in the right way.
So just delete your App in your device/simulator, and rebuild/run it.
Note: Whenever you've modified your models, you need to do it this way unless you offer an approach to handle. There're many related QAs on SO. :)

Related

NSPersistentDocument to be marked as not "dirty"/edited after internal initiation

I have a NSPersistentDocument (CoreData) that I initiate before I present it to the user. That means that I create some internal core data objects and add them to the document/persistent store/managed object context.
However, that means that even if no user activity is happening the document shows the saving dialog when the document is closed. I would like it to be marked as not dirty and no saving dialog as no real change happened.
Any idea? Many thanks in advance!
I handled that problem by implementing this in awakeFromNib:
- (void)awakeFromNib {
// Disable Undo
[self.managedObjectContext processPendingChanges];
[[self undoManager] disableUndoRegistration];
// Do your initialization thing
// Process changes to the object graph and reenable Undo
[self.managedObjectContext processPendingChanges];
[[self undoManager] enableUndoRegistration];
// Rest of awakeFromNib, if any
}
The dirty state is connected to documentEdited. documentEdited is set by updateChangeCount:. updateChangeCount: is automatically called by the undo manager.
Call [[self undoManager] removeAllActions]; or its Swift equivalent to remove the dirty state.

IBOutlet inside document based application

I've created an application document based but I had problem to handle IBOutlet becouse when I selected some controller inside the document in the first window sometime the other same controller in another window document was selected too...
Maybe I did wrong with strong/weak/readwrite or something I'm not very sure so I've decided to force my application to have just one window document for time.
Now my problem. I've an arrayController and when I launch my application and I do open saved file it close the "blank document" created by default and show the new window document with the data loaded.
The problem is that the arrayController of the second window called inside the windowControllerDidLoadNib is the right pointer (created by makeWindowControllers) but when I use this inside the code it has another pointer that is the old arrayController of the blank document that is been closed!! Why? Do close method release everything? (I use ARC).
Excuse me if my words are confused, I'm newbie. Thank you.
This is my code:
Document* newDoc = [[Document alloc] init];
[[NSDocumentController sharedDocumentController]addDocument:newDoc];
[newDoc makeWindowControllers];
[newDoc showWindows];
I've solved this issue: when you define notification for some object as a nsdocument these are not released after close calls. So you need to remove observer!

subclass NSWindowController Core Data

I have a newb question, which I have tried unsuccessfully to find answers for on the web. The task is simple: I want to create a core data document-based app but alter the values in some label objects. Using interface builder, I can build the core data model and populate it, using an array controller, table, etc. all without writing any code. So far so good. My test example is to build a core data model with Box entities that have length and width attributes. I would like a label to display the area, i.e. length*width for any geometrically challenged :).
So after browsing around, I've decided I need to create an NSWindowController subclass and use that to update the label when a box in the table is selected. Have attempted this, but have failed. Before I even hook up the label to the window controller, I have a problem. Following the template comments, I added this to Document.m:
- (void)makeWindowControllers
{
NSLog(#"Adding custom Window Controller");
MyWindowController* myWindowController = [[MyWindowController alloc] init];
[self addWindowController:myWindowController];
}
Also added this to the template MyWindowController.m:
- (id)init
{
self = [super initWithWindowNibName:#"MyWindowController"];
return self;
}
The window controller has its own NIB file from Interface builder where I put the table and label etc. The file owner is set to MyWindowController. Probably forgetting other things, but that's what I remember for now.
The log message appears at startup, but then I get an exception "this class is not key value coding-compliant for the key managedObjectContext" before the window appears. I'm guessing that I don't have the window controller hooked up to the document class properly? My other thought is that the array controller is in the window controller nib, not the document nib, so maybe it's looking in the wrong place for the managedObjectContext?
I would try a different approach: Add area to your Box entity as a read-only attribute. Auto-create a Box class with Xcode ("Create NSManagedObject Subclass" menu item in the "Editor" menu when viewing the data model), then add this to Box.h
#property (weak, readonly) NSString * area;
and this to Box.m
- (NSNumber *)area
{
return [NSNumber numberWithDouble:([[self length] doubleValue] - [[self width] doubleValue])];
}
+ (NSSet *)keyPathsForValuesAffectingArea
{
return [NSSet setWithObjects:#"length", #"width", nil];
}
If you do this you can just bind area to a label value like you do for the other Box properties. No need to subclass NSWindowController or watch for changes.

NSNotification with multithreading XCODE

I am a newbie to IPHONE development. I am facing a issue in working with NSNotification with multithreading.
I have a few images in a gallery.I select the image. Selected images get stored in core data.I have a button (upload). When i click on it i need to show a hud with a NSNotification saying (Uploading with image name). ie. "uploading image1.jpg" then i need to call the next thread to display "uploading image2.jpg" and so on. I need a sample code for this.
I need to know how to send and receive NSNotification with multithreading. Kindly help me in this issue.
Thanks in advance.
Consider using MBProgressHUD for this.
The demo project include examples very similar to what you're doing.
The components also has other feature you may want, like a progress indicator.
From the main page, configuring an async task to have a HUD notification is as simple as:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});

FBConnect - template not using my template bundle

I registered a template bundle for my app, one that only uses *actor*, so I brought it up like this:
FBFeedDialog* dialog = [[[FBFeedDialog alloc] init] autorelease];
dialog.delegate = self;
dialog.templateBundleId = 12345;
[dialog show];
(using my bundle id, of course)
But all I get when the dialog comes up is "Do you want to publish this story to your Profile?". The "story" doesn't show up in the dialog, and if I click Publish I end up with a blank story in my feed.
Then I tried registering another one which a) has only a one-line story, to make things simpler (the first one had everything) and b) uses a custom key.
FBFeedDialog* dialog = [[[FBFeedDialog alloc] init] autorelease];
dialog.templateBundleId = 12345;
dialog.templateData = #"{\"flavor\": \"chocolate chip\"}";
[dialog show];
Same result, blank story. I've done a lot of google searching and can't find anyone else with this problem, so I must be doing something incredibly silly. Can anyone advise, please?
I fixed it, but I don't quite understand the fix (I'm new to Obj-C as well as iPhone).
I have an ivar called session, which stores the FBConnect session, for which I had an #property and #synthesize as usual. I removed both of those and explicitly retained the session when it was allocated, instead of relying on the property to do it, and it started working. I don't see how these are functionally different, but in comparing my code to the sample, which worked, I noticed this difference and tried it. The release is in the dealloc method, which is where it was all along.
I would love an explanation if anyone can give one!

Resources