realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.18k stars 2.14k forks source link

OC 中objectsWhere. IN关键字怎么使用? #7750

Closed PerfectShen closed 2 years ago

PerfectShen commented 2 years ago

How frequently does the bug occur?

All the time

Description

IN 关键字 格式化的时候有问题啊 NSArray *list = [@"1",@"2",@"3",@"4"]; listStr = [list componentsJoinedByString:@","];

[BusiModel objectsWhere:@"name IN {%@}",listStr]; 根本查不出来

Stacktrace & log output

IN 关键字 
格式化的时候有问题啊 
NSArray *list = [@"1",@"2",@"3",@"4"];
listStr = [list componentsJoinedByString:@","];

[BusiModel objectsWhere:@"name IN {%@}",listStr];
根本查不出来

Can you reproduce the bug?

Yes, always

Reproduction Steps

IN 关键字 格式化的时候有问题啊 NSArray *list = [@"1",@"2",@"3",@"4"]; listStr = [list componentsJoinedByString:@","];

[BusiModel objectsWhere:@"name IN {%@}",listStr]; 根本查不出来

Version

1.x

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS14.7

Build environment

Xcode version: 12.5 Dependency manager and version: ... IN 关键字 格式化的时候有问题啊 NSArray *list = [@"1",@"2",@"3",@"4"]; listStr = [list componentsJoinedByString:@","];

[BusiModel objectsWhere:@"name IN {%@}",listStr]; 根本查不出来

leemaguire commented 2 years ago

This is not a valid way of producing an NSPredicate, pass in the array literal instead e.g


[BusiModel objectsWhere:@"'name' IN %@", list]; or [BusiModel objectsWhere:@"'name' IN ['Foo', 'Bar']"];

See also: https://academy.realm.io/posts/nspredicate-cheatsheet/