我正在使用Core Data,并希望将auto_increment ID设置为唯一的字段之一.是否可以使用核心数据在iOS中设置auto_increment?任何人都可以帮我一个如何实现这个的小例子?
下面是我在数据库中插入记录的代码.在第一个字段"id"中,我想将其设置为auto_increment而不是手动插入它.
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
NSManagedObject *newObj = [NSEntityDescription insertNewObjectForEntityForName:@"Users" inManagedObjectContext:context];
[newObj setValue:[NSNumber numberWithInt:userId] forKey:@"id"];
[newObj setValue:theFileName forKey:@"Name"];
Run Code Online (Sandbox Code Playgroud) I was trying to create app shortcut at home. I did that quite well for most devices.
with this
private void setShortcutIcon() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
final ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(this, "334")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setShortLabel(getString(R.string.app_name))
.setIntent(new Intent(this, SplashActivity.class).setAction(Intent.ACTION_VIEW).putExtra("duplicate", false))
.build();
shortcutManager.requestPinShortcut(pinShortcutInfo, null);
} else {
final Intent shortcutIntent = new Intent(getApplicationContext(), SplashActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", …
Run Code Online (Sandbox Code Playgroud) android app-launcher android-appshortcut android-8.1-oreo android-9.0-pie
我是iOS开发的新出价.我正在使用NSManagedObject of Core Data来执行Insert和Fetch操作.它工作得很好.但问题是,我想从表中只获取一些选定的记录(在MySQL中的条件).例如"从city ='Pune'的用户中选择名称"; 我找到了NSPredicate来获取过滤后的数据.但它提供了数组中的所有数据,而不仅仅是所选数据.例如,如果以上查询的结果是:
Anu
Run Code Online (Sandbox Code Playgroud)
然后NSPredicate结果将给出:
fname = Anu
lname = Padhye
city = Pune
id = 3
Run Code Online (Sandbox Code Playgroud)
有没有办法只在iOS Objective-c中获取所选的记录?以下是我用于NSManagedObject的代码片段:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
valueData = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSEntityDescription *productEntity=[NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
[fetch setEntity:productEntity];
NSPredicate *p=[NSPredicate predicateWithFormat:@"id == %d", 3];
[fetch setPredicate:p];
//... add sorts if you want them
NSError *fetchError;
NSArray *fetchedProducts=[valueData filteredArrayUsingPredicate:p];
Run Code Online (Sandbox Code Playgroud)