参考帖子:
核心数据 - 基本问题
我能够从这段代码中获取托管对象上下文。它让我想到另一个问题。我有 2 个视图控制器和 1 个 NSObject
- 用户查找 (VC)
- 用户查找设置(VC)
- FetchProcessor (NSObject)
按顺序,Userlookup vc 首先加载,并且有一个按钮来加载 Userlookupsettings VC + 一个文本框和 UiButton。当应用程序加载并且我点击 SETTINGS uibutton 时,一切正常......但是,当我进行搜索(FetchProcessor)然后加载设置时,它给了我错误(请在下面检查)
if (![[managedObject managedObjectContext] save:&error]) {
NSLog(@"Unresolved error %@, %@, %@", error, [error userInfo],[error localizedDescription]);
exit(-1); // Fail
}
错误:
2010-09-11 03:10:47.148 SAPBasis[975:207] *** -[NSCFString objectID]: unrecognized selector sent to instance 0x3d5d830
2010-09-11 03:10:47.170 SAPBasis[975:207] Serious application error. Exception was caught during Core Data change processing: *** -[NSCFString objectID]: unrecognized selector sent to instance 0x3d5d830 with userInfo (null)
2010-09-11 03:10:47.170 SAPBasis[975:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString objectID]: unrecognized selector sent to instance 0x3d5d830'
编辑并添加了相关代码..
用户查找:
-(void) searchUser{
getUserDetailsService=[[GetUserDetailsSOAPService alloc]init]; // where AbstractServiceProvider *getUserDetailsService; and @interface GetUserDetailsSOAPService : AbstractServiceProvider
[getUserDetailsService setSettingPreference:settings];
[settings release];
[getUserDetailsService setDelegate:self];
RequestDO * request = [[RequestDO alloc]init];
request.userID=userIdInputField.text;
[getUserDetailsService setRequestDO:request];
[request release];
NSManagedObjectContext *context = self.referringObject;
[getUserDetailsService setReferringObject:context];
[getUserDetailsService execute]; // This is the user search function.
[getUserDetailsService release];
}
-(void) editUserLookupSettings{
UserLookupSettings *viewVC = [[UserLookupSettings alloc] initWithNibName:@"UserLookupSettings" bundle:nil];
viewVC.title = @"Settings for User Lookup";
NSManagedObjectContext *context = self.referringObject;
viewVC.referringObject = context;
[self.navigationController pushViewController:viewVC animated:YES];
// Manage memory
[viewVC release];
}
现在@implementation GetUserDetailsSOAPService
-(void)execute{
TCodeSettings *fetch = [[TCodeSettings alloc] init]; // Where @interface TCodeSettings : NSObject <NSFetchedResultsControllerDelegate>
fetch.referringObject = self.referringObject;
resultsOfSettings = [fetch initCode]; // Code details given below. I think so is causing the error when this is called.
[fetch release];
self.userData = [[NSMutableDictionary alloc] init];
self.previewData = [[NSMutableArray alloc] init];
// Creates new Request object and sets its url
NSString *URLReq = [self.settingPreference getSOAPPrefix];
URLReq=[URLReq stringByAppendingString:@"Z_USERLOOKUPWS"];
URLReq=[URLReq stringByAppendingString:[self.settingPreference getSOAPSuffix]];
theRequest=[NSMutableURLRequest
requestWithURL:[NSURL URLWithString:URLReq]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30.0];
// Setting specific SOAP headers
// For SOAP content type is text/xml
.
.
.
[self makeReqest]; // AbstractServiceProvider we have makeRequest function and it works fine..
}
现在@interface TCodeSettings:NSObject
- (NSFetchedResultsController *)initCode{
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle the error
}else {
return fetchedResultsController;
}
}
现在@interface UserLookupSettings:UITableViewController
- (void)viewDidLoad {
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle the error
}
[super viewDidLoad];
}