Zepo / GYDataCenter

An alternative to Core Data for people who like using SQLite directly.
MIT License
842 stars 132 forks source link

[obj save];报错 #4

Closed JayChunlen closed 8 years ago

JayChunlen commented 8 years ago

for (int i = 0; i < 100; i++) { User *u = [[User alloc] init]; u.name = [NSString stringWithFormat:@"name%d",i]; u.uid = [NSString stringWithFormat:@"%d",i+10000]; [u save]; }

* Assertion failure in +[User persistentProperties], /Users/imc/Desktop/1Card1/Pods/GYDataCenter/GYDataCenter/GYModelObject.m:75 2016-07-15 11:05:22.117 1Card1[1318:45981] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Should use method of a subclass.' * First throw call stack: ( 0 CoreFoundation 0x0000000106962d85 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001063d6deb objc_exception_throw + 48 2 CoreFoundation 0x0000000106962bea +[NSException raise:format:arguments:] + 106 3 Foundation 0x0000000106020d5a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 GYDataCenter 0x0000000105f3e6aa +[GYModelObject persistentProperties] + 234 5 GYDataCenter 0x0000000105f3d5a4 +[GYDCUtilities persistentPropertiesForClass:] + 36 6 GYDataCenter 0x0000000105f3e754 +[GYModelObject propertyTypes] + 116 7 GYDataCenter 0x0000000105f3f9a5 +[GYModelObject relationshipGetterNames] + 165 8 GYDataCenter 0x0000000105f3f22b +[GYModelObject resolveInstanceMethod:] + 59 9 libobjc.A.dylib 0x00000001063d5a95 _ZL28_class_resolveInstanceMethodP10objc_classP13objc_selectorP11objc_object + 80 10 libobjc.A.dylib 0x00000001063ddd7e lookUpImpOrForward + 361 11 libobjc.A.dylib 0x00000001063ddb8f class_getInstanceMethod + 52 12 Foundation 0x0000000105f86fd6 NSKeyValueMethodForPattern + 139 13 Foundation 0x0000000105f86de1 +[NSObject(NSKeyValueCodingPrivate) _createValueSetterWithContainerClassID:key:] + 211 14 Foundation 0x0000000105fa115f -[NSObject(NSKeyValueCoding) setValue:forKey:] + 228 15 GYDataCenter 0x0000000105f2adff 28-[GYDataContext saveObject:]_block_invoke + 159 16 libdispatch.dylib 0x00000001095a43eb _dispatch_client_callout + 8 17 libdispatch.dylib 0x0000000109588ef5 _dispatch_barrier_sync_f_invoke + 393 18 GYDataCenter 0x0000000105f278d7 -[GYDataContextQueue dispatchSync:] + 119 19 GYDataCenter 0x0000000105f2acd6 -[GYDataContext saveObject:] + 662 20 GYDataCenter 0x0000000105f404b3 -[GYModelObject save] + 99 21 1Card1 0x0000000105dd5a22 -[ViewController viewDidLoad] + 258 22 UIKit 0x0000000106eb3984 -[UIViewController loadViewIfRequired] + 1198 23 UIKit 0x0000000106ef7fae -[UINavigationController _layoutViewController:] + 54 24 UIKit 0x0000000106ef8882 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 462 25 UIKit 0x0000000106ef89f4 -[UINavigationController _startTransition:fromViewController:toViewController:] + 126 26 UIKit 0x0000000106ef9c4d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890 27 UIKit 0x0000000106efad0b -[UINavigationController viewWillLayoutSubviews] + 57 28 UIKit 0x00000001070a9503 -[UILayoutContainerView layoutSubviews] + 248 29 UIKit 0x0000000106dd3980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 30 QuartzCore 0x000000010bb42c00 -[CALayer layoutSublayers] + 146 31 QuartzCore 0x000000010bb3708e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 32 QuartzCore 0x000000010bb36f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 33 QuartzCore 0x000000010bb2b3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 34 QuartzCore 0x000000010bb59086 _ZN2CA11Transaction6commitEv + 486 35 QuartzCore 0x000000010bb597f8 _ZN2CA11Transaction17observer_callbackEP19CFRunLoopObservermPv + 92 36 CoreFoundation 0x0000000106887c37 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION** + 23 37 CoreFoundation 0x0000000106887ba7 __CFRunLoopDoObservers + 391 38 CoreFoundation 0x000000010687d11c CFRunLoopRunSpecific + 524 39 UIKit 0x0000000106d13f21 -[UIApplication _run] + 402 40 UIKit 0x0000000106d18f09 UIApplicationMain + 171 41 1Card1 0x0000000105dd82cf main + 111 42 libdyld.dylib 0x00000001095d892d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

Zepo commented 8 years ago

User 类没有实现 persistentProperties 方法,或者是因为你调了 [super persistentProperties],不应该调 super。

JayChunlen commented 8 years ago

import <GYDataCenter/GYDataCenter.h>

@interface User : GYModelObject @property (nonatomic, copy) NSString uid; @property (nonatomic, copy) NSString name; @property (nonatomic, strong) NSDate *dateOfBirth; @end

import "User.h"

@implementation User

调用了persistentProperties方法

JayChunlen commented 8 years ago

2016-07-15 12 02 18

Zepo commented 8 years ago

错误信息还是一样的吗?

JayChunlen commented 8 years ago

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<User 0x7fe8a171fc30> valueForUndefinedKey:]: this class is not key value coding-compliant for the key friendId.' * First throw call stack: ( 0 CoreFoundation 0x000000010c41cd85 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010be90deb objc_exception_throw + 48 2 CoreFoundation 0x000000010c41c9c9 -[NSException raise] + 9 3 Foundation 0x000000010baee144 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226 4 Foundation 0x000000010ba44167 -[NSObject(NSKeyValueCoding) valueForKey:] + 280 5 GYDataCenter 0x000000010b9ee7d7 -[GYDBRunner isPrimaryKeyProvidedWithObject:] + 135 6 GYDataCenter 0x000000010b9ed4f3 -[GYDBRunner saveObject:] + 467 7 GYDataCenter 0x000000010b9e4e31 28-[GYDataContext saveObject:]_block_invoke + 209 8 libdispatch.dylib 0x000000010f05e3eb _dispatch_client_callout + 8 9 libdispatch.dylib 0x000000010f042ef5 _dispatch_barrier_sync_f_invoke + 393 10 GYDataCenter 0x000000010b9e18d7 -[GYDataContextQueue dispatchSync:] + 119 11 GYDataCenter 0x000000010b9e4cd6 -[GYDataContext saveObject:] + 662 12 GYDataCenter 0x000000010b9fa4b3 -[GYModelObject save] + 99 13 1Card1 0x000000010b88e91c -[ViewController viewDidLoad] + 332 14 UIKit 0x000000010c96d984 -[UIViewController loadViewIfRequired] + 1198 15 UIKit 0x000000010c9b1fae -[UINavigationController _layoutViewController:] + 54 16 UIKit 0x000000010c9b2882 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 462 17 UIKit 0x000000010c9b29f4 -[UINavigationController _startTransition:fromViewController:toViewController:] + 126 18 UIKit 0x000000010c9b3c4d -[UINavigationController _startDeferredTransitionIfNeeded:] + 890 19 UIKit 0x000000010c9b4d0b -[UINavigationController viewWillLayoutSubviews] + 57 20 UIKit 0x000000010cb63503 -[UILayoutContainerView layoutSubviews] + 248 21 UIKit 0x000000010c88d980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 22 QuartzCore 0x00000001115fcc00 -[CALayer layoutSublayers] + 146 23 QuartzCore 0x00000001115f108e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 24 QuartzCore 0x00000001115f0f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 25 QuartzCore 0x00000001115e53c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 26 QuartzCore 0x0000000111613086 _ZN2CA11Transaction6commitEv + 486 27 QuartzCore 0x00000001116137f8 _ZN2CA11Transaction17observer_callbackEP19CFRunLoopObservermPv + 92 28 CoreFoundation 0x000000010c341c37 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION** + 23 29 CoreFoundation 0x000000010c341ba7 __CFRunLoopDoObservers + 391 30 CoreFoundation 0x000000010c33711c CFRunLoopRunSpecific + 524 31 UIKit 0x000000010c7cdf21 -[UIApplication _run] + 402 32 UIKit 0x000000010c7d2f09 UIApplicationMain + 171 33 1Card1 0x000000010b8912af main + 111 34 libdyld.dylib 0x000000010f09292d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Zepo commented 8 years ago

(NSString *)primaryKey { return @"friendId"; } primaryKey 必须是某个 property

JayChunlen commented 8 years ago

恩恩 ,谢谢

JayChunlen commented 8 years ago

如果我想删除一个表内的所有数据,我不想取出所有数据一个个删除,有没有一个快捷方法一句代码删除?

Zepo commented 8 years ago

[YourModel deleteObjectsWhere:nil arguments:nil];

JayChunlen commented 8 years ago

如果我在搜索条件加两个或多个参数应该怎么查询呢?objectsWhere:[NSString stringWithFormat:@"WHERE superCategoryId = %@ subCategoryId = %@",arr[0],arr[1]] arguments:@[] 我这样用 会报错DB Error: 1 "near "subCategoryId": syntax error" 2016-07-15 17:28:00.761 LittleTail[3621:1256100] DB Query: SELECT * FROM LTPetCategory WHERE superCategoryId = 1 subCategoryId = 503

Zepo commented 8 years ago

不符合 sql 语法。你是要OR? objectsWhere:@"WHERE superCategoryId = ? OR subCategoryId = ?" arguments:@[ arr[0], arr[1] ] 建议用 '?' 转义

JayChunlen commented 8 years ago

OR 的意思是二选一吧,我需要两个条件都符合是不是要用AND objectsWhere:@"WHERE superCategoryId = ? AND subCategoryId = ?" arguments:@[ arr[0], arr[1] ]

Zepo commented 8 years ago

恩,这些都是 SQL 的语法,应该先学一下

lbhbrave commented 8 years ago

能不能设置自增的主键啊?

Zepo commented 8 years ago

整数型的主键默认都是自增的,save 的时候主键填 0 就行了,这时候 save 是同步的,save 完框架内会把主键赋值好。