Re: SQLite Database 2 distinct database iphone.

2010-06-07 Thread Greg Reichow

 Greetings.
 
 I'm building a iPhone application which is database driven.
 in that application i've designed it to have two databases.
 One database will be distributed with the application and is meant to be read 
 only.
 the second database is meant to copy items to it for the user's safe keeping.
 
 the reason for this is that the application update will also include a 
 refreshed database
 and as such if i only link to the records it might happen that the record 
 that the user 
 wished to keep would of been purged from the original database.
 
 evidently the structure is quite the same on both, with the exception of some 
 additional fields
 in the user database.
 
 when the application starts it complains that it can not merge the two models.
 i've been looking on the net but found nothing of significance.
 
 is it possible to have two separate database in the same application on the 
 iphone.
 and what are the steps to make it happen.
 
 do i have to duplicate the Core data initialization procedures and maintain 2 
 distinct managed object contexts?

I have a very similar application requirement.  In my case, I am using 2 
separate persistent stores.   One is the user store located in their documents 
directory to maintain their unique copy of the database and edits to the 
provided data.  The second is part of the application bundle.  In this case, I 
use 2 separate MOC's and migrate data from the app persistent store to the user 
persistent store.  Using the metadata that can be stored with the persistent 
stores, I check a version key I create and then if it is different, do a merge 
of the 2 stores.  See the core data docs for a method for efficiently doing a 
large comparison and merge.  This has worked quite well and allows for 
application database updates without messing up any unique changes the user has 
made to their own earlier copy.  

Doing it this way also protects the user if the application is reloaded.  The 
user data in the documents directory is backed up and can easily be restored.

As for model changes, if they are simple changes to the model between versions, 
you may be able to get away with lightweight migration.  It is a simple option 
to add when loading the persistent store.  

There very well may be a better approach but this has successfully worked for 
me.

Greg Reichow
MangoCode
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Problem with one-way Core Data relationship not persisting

2010-02-21 Thread Greg Reichow
I have a problem with the core data object graph not properly persisting:

Managed Object A (Car) has a one-way, one-to-many relationship to several 
Managed Object B's. (Tires)

When I create the object graph, I use the standard generated relationship set 
mutator methods to setup the relationships:

[carMO addTireObject:tireMO]

After creating the full object graph, I have validated that everything is 
correct.  Reviewing the relationship sets show 4 tires in the set of the car 
relationship. 

Yet, when I save this to the persistent store and either reset the MOC or rerun 
the app and access the previously saved data, everything is there but many of 
the relationships noted above are missing.  *If I switch the type to a 2-way 
inverse relationship (many-to-many), it all works fine *  Without going into 
too much detail, establishing this with the inverse is causing a dramatic and 
not acceptable increase in the time to save and also the size of the store.  
(1M+ cars, 50 tires each for example).  

My question is not around the performance issue with the inverse.  I would like 
to understand why the one-way relationship is not sticking.  I don't need the 
inverse and the cost is too high.  I have tried making sure that all the tires 
are flushed to the persistent store and have permanent ID's first; no change in 
the above.  Also, as I mentioned, it is not 100% of the relationships that are 
missing.  I can not find a pattern and roughly 90% are not sticking.

Any insight into using a one-way one-to-many relationship?  

Thanks,
Greg

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Running NSURLConnection from within an NSOperation?

2010-02-10 Thread Greg Reichow

 I'm trying to run an NSURLConnection from an NSOperation. Apparently, it 
 won't run. I know that NSURLConnection need a run loop. Does that mean I'll 
 have to setup some kind of NSTime in my NSOperation and then call my run 
 loop at regular intervals?
 
 Try using the synchronous version of NSURLConnection since you are launching 
 it from within a NSOperation already-
 
 + (NSData *)sendSynchronousRequest:(NSURLRequest *)request 
 returningResponse:(NSURLResponse**)response error:(NSError **)error
 
 Indeed sendSynchronousRequest: will work for a quick hack to talk to, say, 
 your own server, but if you hope to some day have a real app which 
 predictably handles real-world corner cases, well, let me quote what was told 
 to me once:
 
 The NSErrors that you get from -sendSynchronousRequest: are not documented.  
 For example, if you give it a wrong  username/password combination, you get 
 NSURLErrorUserCancelledAuthentication = -1012.  Apparently this is an 
 implementation detail, that when it receives an authentication challenge, it 
 cancels.  You're leaving the policy decisions to Apple instead of making them 
 yourself, and since they are not documented, Apple may change them at any 
 time.

Ahh yes, the things you learn by posting a bad answer.. thanks for the 
additional insight.

Greg
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Running NSURLConnection from within an NSOperation?

2010-02-09 Thread Greg Reichow

 I'm trying to run an NSURLConnection from an NSOperation. Apparently, it 
 won't run. I know that NSURLConnection need a run loop. Does that mean I'll 
 have to setup some kind of NSTime in my NSOperation and then call my run loop 
 at regular intervals?

Try using the synchronous version of NSURLConnection since you are launching it 
from within a NSOperation already-

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request 
returningResponse:(NSURLResponse**)response error:(NSError **)error


The docs mention that no special run loop configuration is necessary in this 
case.

Greg

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Change background color of UITabBarItem and UITabBar

2010-01-27 Thread Greg Reichow
 
 Thanks for this information. It did change the color of the TabBar.
 But the other question is still unanswered. Is it possible to change
 the TabBarItem's image colors? Instead of default gray (when not selected) 
 and blue (when selected)? How?
 
Again, a little bit of a grey area as you are overriding private methods.  So, 
this very well could break in future updates.  Be warned.  But here is an 
example for your understanding-

Subclass  UITabBarItem and override the private method selectedImage.  

@interface GRCustomUITabBarItem : UITabBarItem 
{
UIImage *customHighlightedImage;
}

@property (nonatomic, retain) UIImage *customHighlightedImage;

@end

@implementation GRCustomUITabBarItem

@synthesize customHighlightedImage;

- (void) dealloc
{   
[customHighlightedImage release]; customHighlightedImage=nil;
[super dealloc];
}

-(UIImage *) selectedImage  
{
return self.customHighlightedImage;
}

@end

Then in the init of each one of the viewControllers that corresponds to a tab, 
you setup the tabBarItem

// Customized Tab Bar Item

GRCustomUITabBarItem *tabItem = [[GRCustomUITabBarItem alloc] 
initWithTitle:@Home image:[UIImage imageNamed:@home.png] tag:0];
tabItem.customHighlightedImage=[UIImage 
imageNamed:@Home-sel.png];
self.tabBarItem=tabItem;
[tabItem release]; tabItem=nil;

The customHighlightedImage should be a png properly sized and colored as you 
like.  It will be used as-is and not recolored to the blue like normal.

Greg
MangoCode

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Change background color of UITabBarItem and UITabBar

2010-01-26 Thread Greg Reichow
 
 Is it possible to change color of UITabBar instead of the default color black?
 And also is it possible to change background color of UITabBarItem's 
 background
 color instead of default color blue? If yes, how?

There is no API for changing the UITabBar.  Yet, you can subclass the 
TabBarController and override the viewDidLoad method.  For example,

@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end

@implementation CustomUITabBarController

- (void)viewDidLoad {
[super viewDidLoad];

CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor redColor]];
[v setAlpha:0.5];
[[self tabBar] addSubview:v];
[v release];
}

@end

This would set a red tint to the tab bar.  Check on stackoverflow.com for many 
more examples.  Keep in mind the above example modifies the tabBar of the 
controller, which is specifically mentioned in the documentation not to mess 
with.  So, use at your own risk. 

Greg
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Core Data Fetch Invariance

2009-12-26 Thread Greg Reichow
Your first comment was correct; they are unordered.  The fact that they may 
come back in the same order is not guaranteed by the API.  Therefore, if you 
need ordering, add an attribute to the core data object to do so.

Greg

On Dec 27, 2009, at 1:38 AM, Richard Somers wrote:

 Objects in a persistent store are unordered. If I fetch the objects, change 
 nothing in the store, then fetch them again, do the two fetches give me the 
 objects in the same order?
 
 --Richard
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 http://lists.apple.com/mailman/options/cocoa-dev/gdr3941%40mac.com
 
 This email sent to gdr3...@mac.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Core Data - Importing using NSOperation - Problems on main thread

2009-12-20 Thread Greg Reichow

 Hi,
 
 I have an application that persists its data using core data.  
 
 It uses an NSOperation to download new versions of the data asynchronously.  
 This NSOperation uses its own MOC (moc2) (as opposed to the MOC (moc1) used 
 in the main thread, which is used by the UI).  Both MOCs share the same 
 persistent store which, as I understand it, this is the paradigm recommended 
 by Apple (for using Core with multiple threads).
 
 My problem occurs when the NSOperation is almost complete and deletes all the 
 old Managed Objects (that have been replaced by the new data) performs a 
 [managedObjectContext save:error] with the intent of making the new data 
 available in the other MOC (moc1).  
 
 The old data is deleted using [managedObjectContext 
 deleteObject:objectToDelete] after the new data has been added. 
 
 At the same point in time as the update is taking place, the user may be 
 scrolling through the data in a UITableView (using custom cells populated 
 with data from managed objects in the main thread MOC moc1)).  The heavy 
 lifting is performed by a NSFetchedResultsController - using code similar to 
 that in the Apple CoreDataBooks example
 
 There are a number of problems with this approach  If the user happens to 
 select the view causing the fetched results controller to be initialised 
 
 i.e.  [self.fetchedResultsController performFetch: error]
 
 whilst the NSOperation is deleting the old Managed Objects, and attempts to 
 scroll an exception is thrown in cellForRowAtIndexPath (as the object that it 
 is referring to has just been deleted)
 
 Problems also occur in the fetched results controller delegate methods, where 
 the didChangeObject methods gets called with NSFetchedResultsChangeUpdate 
 instead of NSFetchedResultsChangeDelete if the user is scrolling up and down 
 the list at the same time (though everything works if the user is not 
 scrolling).
 
 Any ideas on how I can avoid these problems, or come up with a more elegant 
 solution?
 

Are you using the contextDidSaveNotification to pass the updates to the main 
thread MOC?  This has worked for me in the exact same case you describe above.

In your NSOperation, you register for the contextDidSaveNotification (for your 
moc2), which calls a method in the NSOperation when it is received that then 
passes back the notification data to a method on the main thread (using 
performSelectorOnMainThread).  This keeps both MOC's in sync.  I think this is 
documented in the Core Data docs.

Greg
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: [iPhone] Custom UIView like in iPhone Messages App

2009-11-07 Thread Greg Reichow


I am trying to implement a custom ui view with a background image to  
append
chat messages into a scroll view like in iPhone default Messages SMS  
App.

But I have few things to clarify.

If its multiple lines how can I remove the image scaling so that it  
would

not look like that background image is scaled ?

Could anyone suggest some solution for this.


I am not entirely sure what you mean.  Yet, I would suggest using a  
UITableView instead of a UIScrollView to mimic the SMS App style  
message list.  Each message is in a custom cell with custom height  
dependent on the number of lines.  The balloons are an image that is  
just stretched to the proper height.  Using a UITableView will be more  
efficient than loading all messages onto a ScrollView if you have more  
than a screenful of messages in the list.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: an app that never quits

2009-09-22 Thread Greg Reichow


The Apple iPhone Developer Forum is at-

https://devforums.apple.com/community/iphone

You will need to log in using your dev account.  Best of luck with  
your application.  While the lack of background processing is  
problematic for many applications, there is still a large variety of  
apps that can be developed (and hence the 50K+ in the store today...)


Greg



I actually looked for iPhone specific mailing lists ( but didn't  
find any and since it seemed to me that iPhone development is  
actually Cocoa development, I asked here... if you could point me to  
the location of the iPhone mailing lists I'd appreciate it.  I  
looked here:


http://lists.apple.com/mailman/listinfo

but the only iPhone list I found had to do with some government thing.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Core Data Crash on MOC Release

2009-08-13 Thread Greg Reichow

Long time lurker, first time to post.

I have a iPhone 3.0 application that is using core data in an  
NSOperation to perform some updates.  It is using it's own  
NSManagedObjectContext connected to a common (with the main thread)  
persistent store coordinator.  Everything works great until the  
NSOperation ends and is releasing the managed object context.  I get  
an EXC_BAD_ACCESS on the operation thread.  Here is the trace:


#0  0x91a3c688 in objc_msgSend
#1	0x0036c854 in -[_PFManagedObjectReferenceQueue  
_processReferenceQueue:]
#2	0x003a7c40 in -[NSManagedObjectContext(_NSInternalAdditions)  
_dispose:]

#3  0x0040c92d in _deallocateContextBackgroundThread
#4  0x0035f41c in minion_duties2
#5  0x96777155 in _pthread_start
#6  0x96777012 in thread_start

At first I had assumed a simple memory management problem.  The MOC is  
alloc'd by method call from the main method and released in the  
dealloc method of the operation.  New objects are being inserted into  
this MOC  saved (and the main thread MOC being merged) before the  
dealloc.  One clue is that if no new objects are added, it does not  
cause the crash above.  So, my guess is that something bad is  
happening to the managed objects and they are being over-released?   
During the creation of managed objects, at certain intervals a local  
autorelease pool is created, then objects saved, then the pool is  
drained.  Yet, even if the managed objects are released, it does not  
seem that the context should crash on it's own release?


I have spent some time searching on google and found another case of  
this occurring.  Yet, the solution was to set the MOC to retain the  
managed objects.  This did not work in my case.  I have also tried  
forcing the MOC to processPendingChanges and also reseting prior to  
the release to see if that would help, no luck.  Of course,  
eliminating the release of the MOC in the dealloc method did keep it  
from crashing (and everything works great), but this then becomes a  
leak.


Anyone have a similar problem or an idea on how to further figure this  
out?


Here is a cut of the code showing the relevant sections.

.h

#import Foundation/Foundation.h
@interface GRUpdateDatabaseOperation : NSOperation
{
id  delegate;
NSManagedObjectContext  *managedObjectContext;
NSPersistentStoreCoordinator
*persistentStoreCoordinator;
}

@property (nonatomic, assign) id delegate;
@property (nonatomic, readonly) NSManagedObjectContext  
*managedObjectContext;
@property (nonatomic, retain) NSPersistentStoreCoordinator  
*persistentStoreCoordinator;


- (id)initWithDelegate:(id)aDelegate persistentStoreCoordinator: 
(NSPersistentStoreCoordinator *)aPersistentStoreCoordinator;


@end

.m
#import GRUpdateDatabaseOperation.h
@implementation GRUpdateDatabaseOperation

@synthesize delegate;
@synthesize persistentStoreCoordinator;

- (id)initWithDelegate:(id)aDelegate persistentStoreCoordinator: 
(NSPersistentStoreCoordinator *)aPersistentStoreCoordinator

{
if (!(self = [super init])) {
return nil;
}
self.delegate:aDelegate;
self.persistentStoreCoordinator = aPersistentStoreCoordinator;
return self;
}

- (void) dealloc
{
[managedObjectContext release]; // problem with crash is here!
[persistentStoreCoordinator release];
[super dealloc];
}

#pragma mark Core Data Stuff

- (NSManagedObjectContext *) managedObjectContext {

if (managedObjectContext != nil) {
return managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator =  
self.persistentStoreCoordinator;

if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:  
coordinator];
		[managedObjectContext setUndoManager:nil]; // speeds up performance  
for no undo

}

return managedObjectContext;
}

- (void)saveAction:(id)sender {

NSError *error;
if (![[self managedObjectContext] save:error]) {
// Handle error
NSLog(@Unresolved error %@, %@, error, [error userInfo]);
exit(-1);  // Fail
}
}

-(void) contextDidSave:(NSNotification *)notification
{   
if ([self.delegate respondsToSelector:@selector(mergeChanges:)]) {
DLog(@Sending off mergeChanges to main thread);
		[self.delegate performSelectorOnMainThread:@selector(mergeChanges:)  
withObject:notification waitUntilDone:YES];

}
}

#pragma mark Main

- (void)main
{   
	[[NSNotificationCenter defaultCenter] addObserver:self  
selector:@selector(contextDidSave:)  
name:NSManagedObjectContextDidSaveNotification  
object:self.managedObjectContext];


 BUNCH OF CODE HERE TO CREATE CREATE AND INSERT NEW OBJECTS INTO  
MOC ...