领域,在字符串列表中搜索

时间:2019-04-05 10:39:56

标签: android performance realm realm-mobile-platform realm-list

我有一个商品(RealmObject),并且有条形码(RealmList)。 我必须查询哪个项目包含搜索到的条形码。

public class Item extends RealmObject {
    @PrimaryKey
    private long id;
    private RealmList<String> barcodes;

我已经尝试过了,但是它太慢了,因为我有很多物品,并且物品有几个条形码:

for (Item item : realm.where(Item.class).findAll()) {
                    if (item.getBarcodes().contains(barcode)) {
                        itemId = item.getId();
                        return;
                    }
                }

谢谢。

1 个答案:

答案 0 :(得分:1)

不确定原始列表是否支持查询,但是您可以创建新的类// The "sunday" products (setting your product IDS in the array) function sunday_products() { // HERE your product IDs in the array (need to be coma separated) return array( 37 ); } // The 12h to 14h30 products (setting your product IDS in the array) function time_range_products() { // HERE your product IDs in the array (need to be coma separated) return array( 53, 57 ); } // Utility conditional function that check if day is sunday (returns boolean) function is_sunday() { // Set Your shop time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Europe/London'); // If the current day is "sunday" return true (else retun false) return ( date('w') == 0 ) ? true : false; } // Utility conditional funtion for open hours (returns boolean true when store is open) function in_time_range() { // Set Your shop time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Europe/London'); // Below your shop time and dates settings $open_time = mktime('12', '00', '00', date('m'), date('d'), date('Y')); // 12:00:00 $end_time = mktime('14', '30', '00', date('m'), date('d'), date('Y')); // 14:30:00 $now = time(); // Current timestamp in seconds return ( $now >= $start_time && $now <= $end_time ) ? true : false; } // Enable purchases for specific items on sundays only add_filter( 'woocommerce_variation_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 ); add_filter( 'woocommerce_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 ); function enable_specific_products_on_sundays( $purchasable, $product ) { // Enable purchases for specific defined item only on sunday if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) $purchasable = false; // Enable purchases for specific defined item only from 12h to 14h30 if( ! in_time_range() && in_array( $product->get_id(), time_range_products() ) ) $purchasable = false; return $purchasable; } // Add a notice in specific products outside sundays add_action( 'woocommerce_before_single_product', 'filter_before_single_product' ); function filter_before_single_product() { global $product; // For sundays product if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) { wc_print_notice( __( 'This product is only purchasable on sundays', 'woocommerce' ), 'error' ); } // For hour range product elseif( ! in_time_range() && in_array( $product->get_id(), time_range_products() ) ) { wc_print_notice( __( 'This product is only purchasable between 12 AM and 2h30 PM', 'woocommerce' ), 'error' ); } } // IN CASE OF (but not needed): Cart and checkout validation + error message add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' ); add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' ); function conditionally_allowing_checkout() { // Loop through cart items foreach( WC()->cart->get_cart() as $cart_item ){ // Check cart items if( ! is_sunday() && in_array( $cart_item['data']->get_id(), sunday_products() ) ){ wc_add_notice( sprintf(__("%s can be only purchase on sundays.", "woocommerce"), $cart_item['data']->get_name() ), 'error' ); break; } else if( ! in_time_range() && in_array( $cart_item['data']->get_id(), time_range_products() ) ){ wc_add_notice( sprintf(__("%s can be only purchase between 12 AM and 2h30 PM.", "woocommerce"), $cart_item['data']->get_name() ), 'error' ); break; } } } ,该类扩展了Barcode的字符串字段

RealmObject

然后将public class Barcode extends RealmObject{ private String barcodeId; } 替换为RealmList<String> barcodes,然后像这样查询

RealmList<Barcode> barcodes

您还可以添加 @Index 注释,以提高查询速度(但写入速度可能会慢一些)docs

RealmResult<Item> realmResult = realm.where(Item.class).equalTo("barcodes.barcodeId",barcode).findAll();