How to adapt Apple singleton pattern for accessing SQLite database? - iphone

Has anyone developed a singleton for accessing SQLite db? I know that there are other options like Core Data but, in my case, I need SQLite. I have looked at Apple provided singleton creation code (here) but the thing is SQLite database "stuff" is not an object, it is "typedef struct sqlite3". So currently, I'm doubting how should I adapt this code for being singleton. Any suggestions, please :)
UPDATE: I have looked at FMDB framework for SQLite that does all stuff but it doesn't implement singleton. I mean, access methods are instance not class methods. If I need to call the SQL statements from different my code places I need to pass a pointer around instead of calling some shared instance class method :( So, the question remains open.

I think you should use FMDB, it is one of good wrapper libraries around for SQLite3.
See details about FMDB here http://www.ioslib.com/library/data/fmdb/

Related

CoreData web service using MySQL

I currently have a MySQL database that I wish to create a web service for.
One of the main purposes of this web service is to be used in an iPhone app. Because of this I would like to used CoreData, as it will make parsing on the iPhone side so much easier. How would I use CoreData to get the data from my MySQL database? Are there any good tutorials around?
To get the data from the server to the iPhone I would recommend JSON.
Then you need to write some code that will turn that JSON into an object that you can put into the CoreData database. You have a couple choices there, but I would recommend providing your own implementations of the NSCoding protocol. The great part about the NSCoding approach is that the object itself defines what it needs to save/restore one time, then you simply do additional implementations one time to support other formats (e.g. XML, JSON, simple serialization).
Here is the tutorial to sbjson, a JSON parser on Objective-C: sbjson project

Calling custom Objective-C from a pyobjc application?

This question is basically the inverse of this other question: Calling Python from Objective-C
I have implemented my iPhone application logic in Objective-C (obviously), and am now trying to re-use as much as possible from my XCode project in the server component to save on double-implementation. I have successfully loaded the CoreData data model from Python, however, can't see a way to actually call into the Objective-C logic from Python.
Basically I'm trying to access the Objective-C classes and methods in my iPhone project from Python to save myself duping out all the implementations.
Is this even vaguely possible, or is dupe-implementation the only solution here? Seems like the kind of thing Boost::Python might be used for, but I'm not really sure.
edit: Boost::Python won't work because it is C++ based and I need Objective-C. I knew there was a reason why that didn't work.
If your Objective-C code is in a framework and you would like to essentially write a Python application that uses your framework, then you can use objc.loadBundle, and then use objc.lookUpClass or NSClassFromString to get access to your classes. From there, you can use your classes like any other bridged Objective-C class.
If you're running your Python code within a process that already has the Objective-C runtime up, and your classes are already registered with it, then you can skip the loadBundle step.

objectWithFieldName best practices for CoreData

I'm trying to implement some methods for my CoreData models and I'm wondering if the way I'm doing it is a good practice.
I want to implement methods of the type getObjectsWithFieldName. These methods could be used by several views. The way I'm doing it is implementing a Class method in the model as follows:
+(NSArray *)getObjectWithFieldName:(NSString *)fieldName andContext:(NSManagedObjectContext *) context;
Is this a good practice? Or there is another way to do it for iPhone Development?
Short answer, probably not. What class were you planning on adding this to?
If you already have access to the NSManagedObjectModel you can query the model directly to get this information. What class were you planning on adding this to?
Update
That is not going to work very well with Core Data because the designs are different. What is your end goal? You are looking for entities that have a particular property, but why? Since you are writing the code you know what the model looks like, why do you need to query the model to look for the entity?
This design, on its face, does not make sense in Cocoa/Core Data development so some clarification is going to be needed.

How do I create a global variable using Objectvie-C in iPhone?

I'm the beginner in iPhone software development, and I'm creating a HTTP request using the SOAP XML parsing method.
I was creating a variable soapResult for storing the received http data. I want to use the soapResult value into another class.
How can I access that value. I was going to make it a global variable but i don't know how to create and use it?
Also if that method is wrong then how should I implement it?
Generally, singletons are preferred over global variables.
I found this article useful on a similar matter: Cocoa with Love: Singletons, AppDelegates and top-level data
I suggest you to use something like Registry pattern. Just create class Registry with static methods and static properties. It is better way to controll all shared stuff in your app
Global vars are not preferred in objective-c, but if you want to use, this
approach works well.

Object sharing in my iPhone App

I'm currently developping my first iPhone application and I would like to know what is the best way to share an object which is gonna be used in every controller.
I was thinking of referencing the controller containing the needed data in other controllers and access this data thanks to properties, is that a good idea or is there a better way to do it?
Or can we declare any global variable (not const) accessible anywhere in the application?
Thank you
It's not clear from your question what kind of data you are dealing with, but one approach I have used in iphone applications is a singleton object that contains application state and state management functions.
There is a good discussion of the use of singletons versus app delegates for this type of data found at Cocoa With Love and I agree with what he has to say here.
For instance the application in this case had a user account tied to a web application so on load the application would initialize the singleton object and when the user signed in it would keep a reference to the user object in the singleton so that user info could be referenced for api calls.
This approach has worked fairly well for my purposes you can see how to create a singleton class in Objective-C here, and also in the apple docs here there are several ways to do it which provide the same functional result.
Once you have defined your singleton class you can initialize it in applicationDidFinishLaunching or in the viewDidLoad of your main controller and init the globally accessible data you are needing to manage. I highly recommend you read the Cocoa With Love article I linked above for some consideration of managing this whole process.
Hope that helps.
If you're stuck I can post some example code, but the singleton class examples available are pretty straightforward to work with.
If you really need it to be global, you can put it in the AppDelegate or create a data management object. But I think it's frowned upon to do it like that.