2009-10-06 16 views
9

मैंने एक उपयोगिता अनुप्रयोग बनाया है जो लगभग पूरा हो गया है, लेकिन अब मैं एक बिंदु पर हूं कि मुझे वास्तव में डेटा जारी रखना है।मौजूदा उपयोगिता अनुप्रयोग में कोर डेटा कैसे जोड़ें

चूंकि एक्सकोड केवल नेविगेशन या विंडो आधारित ऐप में कोर डेटा टेम्पलेट प्रदान करता है, क्या मेरे आवेदन में कोर डेटा जोड़ने का कोई आसान तरीका है? मैंने कोर डेटा के साथ कभी काम नहीं किया है और केवल संदेश भेजने के इतिहास के रूप में इसे 460 अक्षरों के साथ संदेश जारी रखना है और एक संपर्क नाम टीपी करना है।

या मुझे एक नई विंडो आधारित एप्लिकेशन सहित शुरू करना चाहिए। कोर डेटा और हाथ से उपयोगिता/फ्लिपसाइड भाग बनाने की कोशिश?

क्या कोई मुझे मेरी स्थिति के लिए सबसे अच्छा अभ्यास सुझा सकता है?

उत्तर

9

आपको अपने लक्ष्य में कोरडाटा फ्रेमवर्क जोड़ने, डेटा मॉडल बनाने, और NSManagedObjectModel, NSPersistentStoreCoordinator, और NSManagedObjectContext ऑब्जेक्ट्स को तुरंत चालू करने की आवश्यकता होगी।

एक मौजूदा आवेदन करने के लिए कोर डेटा जोड़ना तुम भी एप्पल tutorial देखने चाहिए क्या शामिल है का एहसास दिलाने के लिए

("मौजूदा अनुप्रयोग" के लिए खोज) this Apple document में संक्षिप्त चर्चा की है।

आप हमेशा SQLite का उपयोग करने पर विचार कर सकते हैं।

+2

मैं बजाय SQLite का उपयोग कर के विचार के साथ सहमत के नाम withe। कोर डेटा बहुत भारी कर्तव्य है और चूंकि ऐसा लगता है कि यहां बहुत अधिक डेटा या कोई जटिल रिश्तों को जारी नहीं किया जा रहा है, तो SQLite ऐसा करने के लिए कम जटिल, अधिक प्रभावी तरीका हो सकता है। –

+3

कोर डेटा आसान है, एक बार जब आप समझते हैं कि यह कैसे काम करता है - यह विशेष रूप से छोटी परियोजनाओं के लिए सच है। –

+4

मैं SQLite के खिलाफ सीधे अपना कोड रोल करने के खिलाफ वोट दूंगा। मैंने जो भी कोड देखा है, उसने कोर डेटा के कुछ हिस्सों को फिर से कार्यान्वित किया है, लेकिन एक छोटी गाड़ी और यहां तक ​​कि कम प्राथमिकता के तरीके में भी। कोर डेटा रास्ता है, खासकर जब सड़क के नीचे कोको टच में बाइंडिंग जोड़े जाते हैं। – PeyloW

6

एक्सकोड में एक नई परियोजना बनाएं, प्रदान किए गए टेम्पलेट का उपयोग करके - उस स्थान को ढूंढें जिसमें स्टोरेज के लिए कोर डेटा का उपयोग करने के लिए चेक बॉक्स है।

जो आपको एक xcdatamodel फ़ाइल देता है, और एप्लिकेशन प्रतिनिधि में कुछ कोड/क्लास वेरिएबल्स देता है कि आप उस प्रोजेक्ट से अपने वर्तमान में कॉपी कर सकते हैं।

मैं भी नल द्वारा वर्णित ऐप्पल ट्यूटोरियल की अत्यधिक अनुशंसा करता हूं।

यदि आप सीधे SQLLite का उपयोग करने का निर्णय लेते हैं, तो दृढ़ता से FMDB का उपयोग करने पर विचार करें, जो SQL कोड को सरल बनाता है। यह एक फ़ाइल है जिसे आप किसी प्रोजेक्ट में जोड़ते हैं।

+0

यदि आप SQLite का उपयोग करने जा रहे हैं तो मैं अत्यधिक एफएमडीबी की भी अनुशंसा करता हूं। –

18

जैसा कि मैं मूल डेटा के twighlightzone को भी समझने की कोशिश कर रहा हूं, मैंने कोर डेटा को 'सामान्य' प्रोजेक्ट माइग्रेट करने के लिए निम्न चरणों का पता लगाया है (मूल डेटा के साथ खाली एप्लिकेशन प्रोजेक्ट के साथ खाली एप्लिकेशन प्रोजेक्ट की तुलना करके)

चरण 1: CoreData.framework

क) "परियोजना लक्ष्य सारांश" "लिंक किए गए फ़्रेमवर्क और पुस्तकालय" के अंतर्गत में + बटन
ख के साथ CoreData.framework जोड़ने जोड़े) फ़ाइल/नई/फ़ाइल का चयन करें और 'कोर डेटा' अनुभाग में एक नया 'डेटा मॉडल' जोड़ें (और इसे कॉल करें यानी XXXXXXX (नामकरण के लिए 3.b देखें)
सी) फ़ाइल में APPLIKATION-Prefix। PCH (जहां आवेदन अपनी परियोजना का नाम है) जोड़ने

#import <CoreData/CoreData.h> 
अन्य दो के तहत

निर्देशों

चरण 2 में शामिल हैं: AppDelegate में।

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; 
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; 
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; 

- (void)saveContext; 
- (NSURL *)applicationDocumentsDirectory; 

चरण 3:: ज निम्नलिखित गुण/विधि घोषणाओं जोड़ने AppDelegate.m

क) Syntesize गुण:

@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 

ख) अंत में मॉड्यूल के निम्नलिखित पंक्तियां जोड़ें:

महत्वपूर्ण: Methode managedObjectModel और persistentStoreCoordinator आप XXXXXXX को बदलने के लिए में अपने व्यक्तिगत .xcdatamodeld फ़ाइल

- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) 
    { 
     if ([managedObjectContext hasChanges] & ![managedObjectContext save:&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. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

#pragma mark - Core Data stack 

/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
*/ 
- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (__managedObjectContext != nil) 
    { 
     return __managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) 
    { 
     __managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [__managedObjectContext setPersistentStoreCoordinator:coordinator]; 
    } 
    return __managedObjectContext; 
} 

/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created from the application's model. 
*/ 
- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (__managedObjectModel != nil) 
    { 
     return __managedObjectModel; 
    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"XXXXXXX" withExtension:@"momd"]; 
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    return __managedObjectModel; 
} 

/** 
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 *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"XXXXXXX.sqlite"]; 

    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil 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. 

     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. 


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

     If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
     * Simply deleting the existing store: 
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
     [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 
} 

#pragma mark - Application's Documents directory 

/** 
Returns the URL to the application's Documents directory. 
*/ 
- (NSURL *)applicationDocumentsDirectory 
{ 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 
+0

फिर प्रबंधित संदर्भ कैसे मॉडल के बारे में पता होगा? खुद ब खुद? – fengd

+0

@ जून 1: xqdatamodel के समान sqlite और momd फ़ाइलों को नामित करना यह है कि यह उदाहरण मॉडल के संदर्भ को कैसे लिंक करता है – ColdLogic

संबंधित मुद्दे