我正在尝试做一些与正在做的事情非常相似的事情 here ,但由于没有给出真正的答案,我想看看是否有人可以帮助我解决我的特定问题。
我只是想将核心数据添加到我现有的应用程序中。 以下是我添加到相应文件中的内容。 我还概述了当我的应用程序加载时我从哪里获得 SIGABRT。 我已验证“loadData”中的上下文变量不为 NULL。
AppDelegate.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : NSObject <UIApplicationDelegate> {
}
...
@property (readonly, retain, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, retain, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, retain, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
-(void)loadData;
@end
AppDelegate.m
#import "NewModel.h"
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
__managedObjectContext = [[NSManagedObjectContext alloc] init];
__managedObjectModel = [[NSManagedObjectModel alloc] init];
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:__persistentStoreCoordinator];
[self loadData];
}
- (NSManagedObjectContext *)managedObjectContext {
if (__managedObjectContext) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
return __managedObjectContext;
}
-(void)loadData
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObjectModel *newObj; // Tried NewModel = *newObj; thinking that may resolve
// the issue, didn't work though
newObj = [NSEntityDescription
insertNewObjectForEntityForName:@"NewModel"
inManagedObjectContext:context]; --> SIGABRT WHEN TRYING TO EXECUTE THIS
[newTeam setValue:@"value" forKey:@"modelValue"];
NSError *error;
[context save:&error];
}
新模型.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface NewModel : NSManagedObject
@property (nonatomic, retain) NSString * modelvalue;
@end
新模型.m
#import "NewModel.h"
@implementation NewModel
@dynamic modelValue;
@end
最佳答案
我上面的代码的问题是我没有正确初始化我的 persistentStoreCoordinator 或 managedObjectModel。以下是曾经添加的两个功能,让我解决了这个问题。
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"LocalDatabase" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
//managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
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;
}
NSString *storePath = [ [self applicationDocumentsDirectory] stringByAppendingPathComponent:@"LocalDatabase.db"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"LocalDatabase" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
关于iphone - SIGABRT 在 NSManagedObjectContext = [NSEntityDescription ...] 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9239349/
给定以下内容,如何获取URL的完整路径uri=URI("http://foo.com/posts?id=30&limit=5#time=1305298413")我只想要posts?id=30&limit=5#time=1305298413我试过uri.path并返回/posts和ui.query返回'id=30&limit=5' 最佳答案 您要找的方法是request_uriuri.request_uri=>"/posts?id=30&limit=5"如果需要,您可以使用任何您想要删除前导/的方法。编辑:要获取#符号后的部分,请使用
$gem--version[/home/rohit/.rvm/gems/ruby-1.9.3-p125@qnrDashboard/specifications/net-ssh-2.5.2.gemspec]isn'taGem::Specification(NilClassinstead).[/home/rohit/.rvm/gems/ruby-1.9.3-p125@qnrDashboard/specifications/net-sftp-2.0.5.gemspec]isn'taGem::Specification(NilClassinstead).[/home/rohit/.rvm/ge
例如,您有一个按优先级排序的项目列表。您有10,000件商品!如果您向用户显示单个项目,您如何为用户提供按钮以查看上一个项目或下一个项目(这些项目是什么)?您可以将项目的位置传递到项目页面并在SQL查询中使用OFFSET。这样做的缺点是,除了必须传递一个可能会改变的数字之外,数据库无法跳转到偏移量;它必须读取每条记录,直到到达第9001条记录。这很慢。寻找解决方案后,我找不到,所以我写了order_query.order_query使用相同的ORDERBY查询,但还包括一个WHERE子句,该子句排除当前记录之前(对于下一个)或之后(对于上一个)的记录。下面是标准的示例(使用上面的gem
我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符
如果这是一个愚蠢的问题,我很抱歉,但我是一个在ruby周围摸索的C#人..在ruby中,我注意到很多人这样做:do_something(withparams)if1=1那个和这个之间有什么区别吗(甚至是轻微的):if1=1do_something(withparams)还是为了更清楚而写的相同内容? 最佳答案 后者在句法上是无效的。你需要写:if1==1thendo_something(withparams)end单行条件句必须始终尾随。是的,有区别。试试这些:bar1=iffoo1=14foo1*3end#=>42bar2
有一个gem,它附加一个before_filter到Rails应用:classRailtie这是应用程序中的一些Controller:classDesktopsController现在的问题是,来自gem的before_filter被放入来自DesktopsController的before_filter之前的过滤器链中:DesktopsController._process_action_callbacks.select{|c|c.kind==:before}.collect{|filter|filter.filter}=>[[0]:set_locale,[1]:set_langua
我在已经转义的文本文件中有一些json:"{\"嘿\":\"那里\"}"当我尝试读取文件时:File.open("\file\path.txt").read它再次转义内容,因此现在是双重转义:"\"{\\\"嘿\\\":\\\"那里\\\"}\""有没有办法防止转义?或者,是否有一种简单的方法可以在读取和转义后对字符串进行转义?谢谢。编辑:答案是有道理的,但我似乎无法解析JSON。irb(main):018:0>json=>"\"{\\\"hey\\\":\\\"there\\\"}\"\n"irb(main):019:0>putsjson"{\"hey\":\"there\"}"=>
我正在将开发中的应用程序从Rails4.2升级到Rails5beta1.1。应用程序在升级前运行良好。我已经完成了基本的升级步骤(更新Ruby、更新Rails和相关步骤:http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html)。Gemfile也已更新为最新的Gems。当我运行$railsmiddleware或$railsconsole或$railsserver时,出现以下错误:Nosuchmiddlewaretoinsertafter:ActionDispatch::ParamsParser.../.rvm/gems
我开发了Sinatra应用程序并在那里使用ActiveRecord来处理数据库,但我遇到了一个问题。我为一个模型写了一个测试,它打破了SQLite3::CantOpenException:unabletoopendatabasefile使用以下代码在test_helper.rb中建立与数据库的连接:Dir.chdir('..')doActiveRecord::Base.establish_connection(db_config)end和ActiveRecord::Base.connected?为假。例如,如果我在连接建立后调用User.find(:all),测试将通过并且Active
我有一个使用deviseonrails3的应用程序。我想启用http身份验证,以便我可以从iPhone应用程序向我的网络应用程序进行身份验证。如何从我的iPhone应用程序进行身份验证以进行设计?这安全吗?还是我应该进行不同的身份验证? 最佳答案 从设计的角度来看,您有3个选择:1)使用基本的http身份验证:您的iPhone应用程序有一个secretkey-这是在您的iPhone应用程序代码中烘焙的-用于对网络应用程序的每个请求进行身份验证。Google搜索:“设计基本的http身份验证”2)您可以通过在您的iPhone应用程序中