لماذا لا يمكنني الحصول على السياق من المندوب مع هذا الرمز؟

StackOverflow https://stackoverflow.com/questions/3590736

سؤال

أحصل على خطأ غامض بشكل مزعج من الكود التالي:

GFree2AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
context = [delegate managedObjectContext];

context يتم تعريفه على أنه أ NSManagedObjectContext في ملف .h وهو نفسه في المندوب. يبدو أن جميع الملفات الصحيحة مدرجة (باستثناء <CoreData/CoreData.h> في ملف .M - لكن البرنامج يلقي نفس المشكلة سواء تم تضمينه أم لا. المدرجة في ملف الرأس.)

يتم تضمين جميع الأطر والأشياء الصحيحة - عندما بدأت المشروع ، اخترت "استخدام Coredata لإدارة البيانات" أو أيا كان. بالتأكيد لا ينبغي أن تكون هناك مشكلة؟

هل هناك طريقة أفضل لفعل ما أحاول فعله؟ في الأساس ، لا أريد أن أستمر في تمرير السياق من خلال فصول مختلفة حتى أرغب في النهاية في استخدامه (تخزين البيانات جزء صغير من طلبي).

في وحدة التحكم ، الخطأ الذي أحصل عليه هو:

    2010-08-28 13:09:24.726 GFree2[3912:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
...

    terminate called after throwing an instance of 'NSException'
    Program received signal:  “SIGABRT”.

إذا علقت على السطر: context = [delegate managedObjectContext]; ثم يبدو أن كل شيء يعمل بشكل جيد. (في الوقت الحالي ، لم أستخدم أي كوراتا أو أي شيء ، لذلك لا يوجد المزيد من الكود المتعلق به.

شكرا لأي شخص يمكنه المساعدة ، أو تقديم نظرة ثاقبة على هذا - معقد للغاية.

تحرير: أساليب ملف مفوض التطبيق الخاصة بي:

#pragma mark -
#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;
  }

  NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"GFree" ofType:@"momd"];
  NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
  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 = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"GFree.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. 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.

      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 -
#pragma mark Application's Documents directory

/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
  return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

تحرير مرة أخرى:

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"GFree" ofType:@"momd"];
    NSURL *modelURL = [NSURL fileURLWithPath:modelPath];

هو الخط الذي يكون فيه الخطأ. لست متأكدًا من نوع الملف الذي هذا ، لكنني متأكد من أنه من الواضح أنه لا يجد ذلك أو شيء من هذا القبيل ...:/ أي أفكار ما من المفترض أن أفعله؟

kiamlaluno - آسف لتراجع التعديلات الخاصة بك ، لم يكن يعني ذلك ، مجرد فضول لمعرفة ما كنت قد غيرت واعتقدت أن هذا سيظهر لي ... لكنه أزالها تماما على ما يبدو. لول.

تحرير نتائج البناء:

DataModelCompile build/Debug-iphonesimulator/GFree2.app/GFree2.mom GFree2.xcdatamodel
cd "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2"
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/usr/bin/momc -XD_MOMC_TARGET_VERSION=10.6 "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2/GFree2.xcdatamodel" "/Volumes/files/Gluten Free Cooking/Tom iPhone App/GFree2/build/Debug-iphonesimulator/GFree2.app/GFree2.mom"
هل كانت مفيدة؟

المحلول

أعتقد أن المشكلة في persistentStoreCoordinator الطريقة في الخاص بك GFree2AppDelegate.m ملف.

هل يمكنك تحديث سؤالك باستخدام الكود الدقيق لـ managedObjectModel, managedObjectContext و persistentStoreCoordinator طرق من هذا الملف؟

يتم إنشاء هذه الطرق بواسطة Xcode عند التحقق من "استخدام البيانات الأساسية للتخزين".

بالمناسبة ، يجب ألا تستورد ملفات .m.

نصائح أخرى

أنت تحصل على الخطأ لأن gfree.momd غير موجود. من المستندات الخاصة بـ NSURL's Pathforresource: OFTYPE: الطريقة:

قيمة الإرجاع اسم المسار الكامل لملف الموارد أو لا شيء إذا كان لا يمكن تحديد الملف.

يتم إنشاء gfree.momd في وقت البناء من ملف .xcdatamodeld الخاص بك (انظر في نتائج الإنشاء للخط الذي يبدأ datamodelversioncompile وتوسيعه). هل قمت بحذف هذا الملف أو إعادة تسميتك؟

هل لا يزال مدرجًا في قسم مصادر التجميع في هدف البناء الخاص بك؟ (في Xcode قم بتوسيع الأهداف / [AppName] / مجموعة المصادر).

لاحظت للتو أن سجل تصحيح الأخطاء الخاص بك يقول أن التطبيق يسمى gfree2 ولكن الكود الخاص بك يبحث عن gfree.momd. هل أعدت تسمية المشروع؟ إذا تم تسمية ملف نموذج البيانات الآن gfree2.xcdatamodeld ، فأنت بحاجة إلى تغيير السطر الذي يستورد ملف momd لاستخدام اسم Gfree2 الجديد أيضًا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top